linuxbashhidden-files

Linux Bash Script unhiding files in the directory


Am trying to make a script to unhide files in the directory with parameter --unhide but it is not working. May you help me? Here is my part of the code.

for i in `ls -1`
do
    if [ -f $i ] || [ -d $i ]
    then
        if [ `echo $i | cut -c1` == "." ]
        then
            mv $i ${i#.}
        fi
    fi
done

Thanks!

And how about unhide then i write --unhide file1 file2?

elif [ $1 = --unhide ] && [ ! $2 = --all ]
then
for i in $@
do
if [ -f $i ] || [ -d $i ]
then
if [ `echo $i | cut -c1` == "." ]
then
mv $i ${i#.}
fi
fi
done    

Its easy then we have not hidden files, then parameter name is the same name with the file name. But how to check hidden files? I tried to write --unhide .file1 .file2 but script only unhides .file1 and .file2 not.


Solution

  • So basically what you are asking for is a script that rename files within a directory, where the files start with a '.'?

    Something like the following should work

    GLOBIGNORE=".:.."
    for file in .*; do
       mv -n "$file" "${file#.}"
    done