MandrivaUsers.org : Unhide files - MandrivaUsers.org

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Unhide files

#1 User is offline   cptaylor 

  • New Here
  • Group: Members
  • Posts: 29
  • Joined: 19-November 03

Posted 15 December 2003 - 05:39 PM

I'm sure this is a silly question but I can't figure out how to unhide files via the shell or script. D4X hides partially completed downloads. It doesn't always unhide them properly when complete. I have tried using 'mv' and 'rename' without success. They both do not see the hidden files. Is there a way to get either one to see and rename the hidden files.

Thanks.
0

#2 User is offline   fuzzylizard 

  • MUB Addict
  • Group: Members
  • Posts: 803
  • Joined: 23-September 02

Posted 15 December 2003 - 05:54 PM

As far as I know, the only thing that makes a file hidden in Linux is a decimal point in front of the name, i.e. .kde would make the kde directory hidden.

The way to see all hidden files is by simply using the:

$ ls -a

command. This will show all files that are in a directory. To unhide a file, simply remove the period at the start of the name.

I have not heard of any other way of hidding files in linux, but I may be wrong about that.
The universe began with a word. But which came first: the word or the thought behind the word? You can't create language without thought, and you can't conceive a thought without language, so which created the other, and thus created the universe?
---
Registered Linux User #269723 | www.fuzzylizard.com | My Desktop
0

#3 User is offline   cptaylor 

  • New Here
  • Group: Members
  • Posts: 29
  • Joined: 19-November 03

Posted 16 December 2003 - 11:38 PM

You're right, the files do just have a dot at the beginning. I am able to list the files and can rename them through Konquerer. However, I'd like to rename them from a script. The 'mv' and 'rename' commands do not see them since they are hidden. Perhaps, there's a way to pipe the results of 'ls -a' into the 'mv' command?

I probably should point out that I'm trying to use wildcards. For example, the following commands would not find the hidden file: '.foo.bar'.

mv .* *

This post has been edited by cptaylor: 16 December 2003 - 11:43 PM

0

#4 User is offline   johnnyv 

  • Cute_teddy_bear
  • Group: Members
  • Posts: 961
  • Joined: 17-September 02

Posted 17 December 2003 - 12:45 AM

mv .* * would move all files starting with . (wont move . and ..) to the next directory

eg) if you have a directory test/ and in test/ is a file .blah and directories test2/ and test/3

performing mv .* * in test/ will move .blah into the directory test2/

if you try mv .* * in a directory without any child directories it will result in an error about the last command must be a directory.

Even if you only have one .name file it will always think there are multiple files because every directory has . and .. which are for directory navigation i think, and because there are multiple files mv always wants the second argument to be a directory.
Blah Blah Yackarty Smackarty!
0

#5 User is offline   fuzzylizard 

  • MUB Addict
  • Group: Members
  • Posts: 803
  • Joined: 23-September 02

Posted 17 December 2003 - 02:44 AM

If you want to do this is a script, you are probably going to have to come up with one that inspects each file in a directory, checks for a proceeding dot, assigns the name of the file to a variable and then strips the proceeding dot out of the name and uses the value of the variable in the move command.

I am not sure if shell script can do this, but something like perl, python or ruby could do it with ease.
The universe began with a word. But which came first: the word or the thought behind the word? You can't create language without thought, and you can't conceive a thought without language, so which created the other, and thus created the universe?
---
Registered Linux User #269723 | www.fuzzylizard.com | My Desktop
0

#6 User is offline   WilliamS 

  • Holy Hermit!
  • Group: OTW
  • Posts: 209
  • Joined: 26-May 03

Posted 17 December 2003 - 03:11 AM

Just my curiousity, but why not use Konqueror?
0

#7 User is offline   johnnyv 

  • Cute_teddy_bear
  • Group: Members
  • Posts: 961
  • Joined: 17-September 02

Posted 17 December 2003 - 05:56 AM

I'm trying to do it via the command line but its hard, you can't seem to assign variables from within a pipe, least its not working for me, besides can you even use variables for the source and destination in mv.
Will look into it further after i cook and eat dinner.

[john@bob test]$ ls -a | grep -i -h '^[[:punct:]]\w' | xargs -i sh -c "(echo {} | sed -e 's/\.//g'  -e 's/ /\ /g'; echo {})"
cat
.cat
cathat
.cat.hat
hat
.hat
[john@bob test]$

This post has been edited by johnnyv: 17 December 2003 - 05:56 AM

Blah Blah Yackarty Smackarty!
0

#8 User is offline   Steve Scrimpshire 

  • This is a dark ride
  • Group: OTW
  • Posts: 3,590
  • Joined: 13-December 02

Posted 17 December 2003 - 08:05 AM

Holy cow that's a long command. i bet aru can come along and give you a better command that has like 9 characters...lol.
Posted Image
0

#9 User is offline   johnnyv 

  • Cute_teddy_bear
  • Group: Members
  • Posts: 961
  • Joined: 17-September 02

Posted 17 December 2003 - 09:41 PM

You think thats long look at this ;)/>
but php is much much much much easier/faster to produce for me then sh scripts

create a text file call it mv_hidden

paste this into it:
#!/usr/bin/php -q
<?php

if($_SERVER['argc'] != 2)
{
die("You must enter a directory as an argument!\nUsage: mv_hidden <directoryname>\n");
}

$path = $_SERVER['argv'][1]; // retrieve the path argument
// check path and make sure it has a trailing /, if not add one ---------------------------/
$path_length = strlen($path); // length of path string
$path_length--;
$path_last_slash = strrpos($path,"/"); // last position of / character
if($path_last_slash === false)
{
$path .= "/";
}
else
{
	if($path_length != $path_last_slash)
	{
	$path .= "/";
	}
}
//------------------------------------------------------------------------------------------/
echo "Path to search: $path\n";
$counter = 0;
if($handle = @opendir($path))
{
	while(false !== ($file = readdir($handle)))
  {
	$type = filetype("$path$file");
  if($type == "file")
  {
 	 if(strpos($file,".") === 0)
 	 {
 	 $new_name = substr($file, 1);
 	 $command = shell_exec("/bin/mv $path$file $path$new_name 2>&1");
    if($command != "")// if we got an error display it
    {
    echo $command."\n";
    }
    else
    {
    echo "$file   -->   $new_name\n";
    $counter++;
    }
 	 }
  }
	}
	if($counter != 0)
	{
	echo "$counter files converted\n";
	}

closedir($handle);
}
else
{
die("'$path' is not a valid directory\n");
}
?>


save the file then make it executable, make sure php is installed, place the mv_hidden in your $PATH some where (perhaps /usr/bin)

Then cd to a directory you want to convert all .blah files to blah
The command is mv_hidden /path
Blah Blah Yackarty Smackarty!
0

#10 User is offline   johnnyv 

  • Cute_teddy_bear
  • Group: Members
  • Posts: 961
  • Joined: 17-September 02

Posted 17 December 2003 - 09:44 PM

Oh by the way this is dangerous . files are usually configuration files so you can fsck up your box if you go messing with them.
YOU HAVE BEEN WARNED! :P/>

Oh and the script will overwrite files with the same name as your . file without the dot if you have write permission for that file.

eg .cat and cat
cat gets overwritten by .cat

This post has been edited by johnnyv: 17 December 2003 - 09:51 PM

Blah Blah Yackarty Smackarty!
0

#11 User is offline   Cannonfodder 

  • Honorary Admin
  • Group: Members
  • Posts: 3,038
  • Joined: 07-September 02

Posted 18 December 2003 - 03:30 AM

While the scripting can be fun and a good learning experience, have to ask why you are renaming the files? Period or no.. is there something wrong with their current file name?
"Have you kissed your local Propeller Head today?"
The answers to Frequently Asked Questions
Posted Image
0

#12 User is offline   Qchem 

  • Mandriva Guru
  • Group: Members
  • Posts: 1,757
  • Joined: 02-October 03

Posted 18 December 2003 - 01:20 PM

Would this not be better as some form of loop?? eg

#!/bin/bash
for OLDNAME in ./.*
do
NEWNAME=`echo OLDNAME | cut -d. -f3`
mv $OLDNAME $NEWNAME
done
exit 0


Or possibly using tr as opposed to cut.....

BTW hidden files are usually that for a reason! This script is untested, if you use it and it breaks something - YOU own the pieces.
Grant
0

#13 User is offline   AlgorithMan 

  • New Here
  • Group: New here
  • Posts: 3
  • Joined: 09-October 06

Posted 10 March 2008 - 06:05 PM

I stumbled upon this thread because I want to do the same thing - my motivation is i want to hide/unhide empty/nonempty subdirectories of my ~/Videos directory (another script might put files into the hidden directories) so "not-doing it" or using GUIs is not an option... after reading through this thread I have to say, ALL proposals here are nonsense!

"cut -d. -f3" will mess up every file that has a name with more than one . in it
using php breaks butterflies on a wheel
"mv .* *" won't work at all (as pointed out before)
echo "$filename" | sed s/\.// would remove the first character from a filename - no matter, if it's a . or not (since \. is an arbitrary character)
echo "$filename" | sed s/\\.// would remove the first . from a filename - if the file isn't hidden it would remove a different . (e.g. the file-extension separator)

I wrote my own solution now - I also use sed, but only on ^\\. so it will only remove a . if it is the first character (this is indicated by the ^ )
using ^\. would remove the first character, since \. is an arbitrary character - combined with ^ would be the first character

mv "$f" "$( echo "$f" | sed s/^\\.// )"

this will unhide a file, but only if it has no directory in front of it... i think in that case

mv "$f" "$(dirname "$f")/$(basename "$f" | sed s/^\\.//)"

should do. Edit: yes, that one works nicely

This post has been edited by AlgorithMan: 14 March 2008 - 11:27 PM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users