LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Directory changed to file name (https://www.linuxquestions.org/questions/linux-newbie-8/directory-changed-to-file-name-921564/)

bigger_travis 01-02-2012 12:11 AM

Directory changed to file name
 
hello, i am new to linux and i dont know what i did.

i had a very important directory called public_html for my website i was moving a file to my /home/user21/public_html/ directory with this command:


mv mybackup.sql /home/user*/pub*/mybackup.sql

so when i cd to the /home/user21/public_html/ it said "no such directory"

so when i do a list it shows the public_html as a file?

Code:

-rw-r--r--  1 root              root                44966003 2012-01-01 17:26 public_html
Notice on the permission, it has -rw-r--r-- where as my other directories have drwxrwxrwx

can anyone help me please. how can i change it back to a directory?

thank you

ongte 01-02-2012 01:47 AM

You basically just renamed your mybackup.sql to public_html. You probably shouldn't be using wildcards in your mv command.

Just rename back your file with mv.

bigger_travis 01-02-2012 03:07 AM

oh i see. ok.
so if i rename public_html to mybackup.sql

Code:

mv public_html mybackup.sql
ill be able to get my mybackup.sql file,

im scared to even ask this.. what happened to the original public_html, directory is it gone? anyway i can get it back?

ongte 01-02-2012 07:29 AM

Yes. Your mv command will restore mybackup.sql

As for your folder.. I've never used wildcards in a command like this, I'm not sure what it will do.

I'm inclined to believe that the public_html folder did not exist in that location in the 1st place, otherwise it would not have renamed the file like it did.

MTK358 01-02-2012 07:30 AM

Quote:

Originally Posted by bigger_travis (Post 4563540)
mv mybackup.sql /home/user*/pub*/mybackup.sql

Do not use wildcards to reduce typing for a single filename. It might accidentally match more than one filename that you didn't think of. Use tab completion instead (enter the first few chars of a filename and press Tab. bash will complete it if there is only one file starting with those characters).

David the H. 01-02-2012 05:24 PM

The shell expands wildcards (globs) into the complete list of matching files before it executes the command. So the actual command that the shell ran was something like this:

Code:

mv mybackup.sql /home/user1/pub1/mybackup.sql /home/user1/pub2/mybackup.sql /home/user2/pub1/mybackup.sql ... ... /home/user100/pub100/mybackup.sql
With the whole list depending on how many files and directories are matched with * (Tack echo to the front of the command to see what it looks like expanded).

Depending on how the final command is formatted, you can end up overwriting files of the same name, or moving whole directory trees to other places.

A safer way to use mv in cases like this is to explicitly name the target directory with the -t option.

Code:

mv -t targetdir mybackup.sql
Also consider using the -v (verbal) option, and either the -i (interactive), or -n (no-clobber) options in mv and cp. Set them up as aliases or shell functions so they're always active by default.

bigger_travis 01-03-2012 02:10 PM

yes, i agree. i took a gamble using the wild card. i will remember that next time not to be tempted to use it. for the last couple of day i have been on that shell sending command after command trying to see what i did wrong. i prayed that i can get this data back. it was 5.3G worth of files.. i review my history. used the find command and the fgrep command to see where i can find it. FINALLY. i found something strange. i looked for all the public_html files in the whole file system and i found one that caught my eye. i dont know what i did, but i guess i moved the public_html directory to another public_html directory because thats where i found it. it was amazing when i saw all those files back. I kneel down and thanked that Lord Jesus for getting back this data. Thank you "ongte", "MTK358" and "David the H." for your advise. i will use the wild card more carefully next time.


All times are GMT -5. The time now is 07:36 PM.