Unhide files
#1
Posted 15 December 2003 - 05:39 PM
Thanks.
#2
Posted 15 December 2003 - 05:54 PM
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.
---
Registered Linux User #269723 | www.fuzzylizard.com | My Desktop
#3
Posted 16 December 2003 - 11:38 PM
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
#4
Posted 17 December 2003 - 12:45 AM
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.
#5
Posted 17 December 2003 - 02:44 AM
I am not sure if shell script can do this, but something like perl, python or ruby could do it with ease.
---
Registered Linux User #269723 | www.fuzzylizard.com | My Desktop
#6
Posted 17 December 2003 - 03:11 AM
#7
Posted 17 December 2003 - 05:56 AM
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
#8
Posted 17 December 2003 - 08:05 AM
#9
Posted 17 December 2003 - 09:41 PM
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
#10
Posted 17 December 2003 - 09:44 PM
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
#11
Posted 18 December 2003 - 03:30 AM
#12
Posted 18 December 2003 - 01:20 PM
#!/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.
#13
Posted 10 March 2008 - 06:05 PM
"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

Help
MultiQuote









