Moving Hidden files to normal files

I have a bunch of hidden files in a directory in AIX. I would like to move these hidden files as regular files to another directory.

Say i have the following files in directory /x

.test~1234~567
.report~5678~123
.find~9876~576

i would like to move them to directory /y as

test~1234~567
report~5678~123
find~9876~576

Can you please let me know the best way to do this ? Appreciate your help.

Warm Regards.

Try:

#!/bin/ksh
for i in .[^.]*
do      
     echo mv "$i" "${i#.}"
done

I use the Korn shell. You can use any POSIX conforming shell by changing /bin/ksh in #!/bin/ksh to the pathname of the conforming shell you want to use. (And, remove the echo , when you have convinced yourself that the script correctly selects the files you want to move.)

1 Like

Thanks Don.

I tried it to a sample hidden file, but the for loop doesnt identify the hidden file at all. I get the following when i run it:

$ ~/mvhidden
mv .. .

Following are the files in the directory

$ls -la
 
.test~HJKL~1234~435
.test~ABCD~5678~123
..
.

I want the above two hidden files to be converted to unhidden so that i can move them to another directory as unhidden.

I have your script like the following:

for i in .[^.]*
do
  echo mv "$i" "${i#.}"
done

Please help.

You cannot do that. "." and ".." are special. A single "." denotes the current directory and ".." denotes it's parent directory.

Assuming all your file names are more than 2 char length:

#!/bin/ksh

printf "%s\n" .* | while read h_file                    # Reading all hidden file names
do
        r_file=$( echo "$h_file" | sed 's#^\.##g' )     # Removing the leading period sign
        if [ ${#r_file} -gt 2 ]                         # Checking if length is greater than 2
        then
                echo "mv $h_file $r_file"
        fi
done

Note: Remove the highlighted echo if the output is as expected.

1 Like

Spot on, bipinajith. Worked like charm.

Thanks a bunch for your help.:slight_smile:

I note that you left out the #!/bin/ksh line that I had in the script I gave you.

In my test directory, where the output from the command:

ls -1ad .*

is:

.
..
.find~9876~576
.report~5678~123
.test~1234~567

the output from my script running on OS X is:

mv .find~9876~576 find~9876~576
mv .report~5678~123 report~5678~123
mv .test~1234~567 test~1234~567

I'm glad that bipinajith's script worked for you, but I'd still like to know why ksh or bash on AIX didn't work for you. What shell did you use? What happens if you run the command:

ksh NameOfFileContainingMyScript

instead of:

~/mvhidden

Note that ~/mvhidden runs a command found in your home directory; did you by any chance have different versions of mvhidden in different directories?

Thanks for your reply, Don,

I tried the following as you have mentioned in your latest reply.

$ ksh /u/home/unarayan/mvhidden_don

I get the following

mv .. .

I have the following hidden files in the path

-rw-rw-rw-   1 xyz   users  17 2012-12-21 23:07 .test~HJKL~1234~435
-rw-r--r--   1 xyz    users  15 2012-12-22 07:32 .test~ABCD~5678~123

But still the files did not get converted to unhidden one when i run your script.

I have the following in mvhidden_don

#!/usr/bin/ksh
for i in .[^.]*
do
     echo mv "$i" "${i#.}"
done

Best Regards.

1 Like

Ouch. Yes. I must have been asleep when I made that suggestion. The pattern I suggested used regular expression syntax; not pathname expansion syntax. I don't know why it did what I expected on OS X.

If you change the line:

for i in .[^.]*

to:

for i in .[!.]*

it should do what you wanted.

There are two ksh standards, the old one (I forget what the year was, 1985?), and the new one - ksh93 - from 1993 obviously.
Perhaps if if added "hash-bang" ksh93 to the first script it will work the same as on OS-X.

#!/usr/bin/ksh93
....

Since we are testing things that surprise us! :slight_smile:

I tried it with various shells. ^ only worked with bash (at least 3 and up), recent ksh93 (not older ksh93) and zsh.

1 Like