How to move and create a directory at the same time.
Hi,
I am trying to clean my computer. Basically I try to order some movies to put all of them in directories. I started by hand but I am loosing time I guess. I want to do something like: mv *.avi /nonexisting_directory/ nonexisting_directory being the name of the file without .avi, changing for each file. How can I write in command lineor in script? Thanks for your support. |
Quote:
Code:
for f in *.avi Code:
for f in *.avi |
Code:
for file in *.avi; do To learn the basics of scripting, check out these links: http://www.linuxcommand.org/index.php http://tldp.org/LDP/Bash-Beginners-G...tml/index.html |
Thank you guys.
On David Script the option -t and -n does not seem to be recognized. May be because I work in SSH on a NAS which might not have all options. The script from vonniber is working on "clean" files. I mean space, parenthesis, points are screwing the scripts, which most of my movies files includes. However it does the work as expected on files with clean name like movie.avi . Is there any ' ' to add in the script to correct this matter? I have a certain amount of empty directories now. I tried the following without success: Code:
find -type d -empty -exec rmdir {} \; This one cleaned empty dir: Code:
find ./ -type d -exec rmdir 2>/dev/null {} \; |
You can replace spaces from the file names with the character of your choice (say _) using the following command -
$ for i in *.avi; do rename 's/ /_/g' $i; done If your filenames contain other characters like parenthesis and underscores, it really doesn't matter. Only, they should contain just a single period '.' and that too just before the extension avi. Once the files are cleaned up, they can me moved to their individual directories using - $ for i in *.avi; do file=`echo $i|cut -d '.' -f 1`; mkdir $file/; cp $i $file/; done the cut will extract the filename before the extension (provided each file name has only one '.' and that too just before the extension avi). For example if the filename is "My_Story.avi", then $file = My_Story. Then the cp will copy My_Story.avi to the folder My_Story/ after creating the folder. I hope this works in you case, it certainly worked in my case. |
Quote:
Code:
for f in *.avi |
Thanks for the feedback.
The behavior of the vonbiber script is a bit strange. May be it was not perfect yesterday on some files but I did not catch it. Correct directory are created but they go into root folder /. Which I will have to clean carefully. File *.avi are then renamed into *. For the other script the rename function is not recognized by my nas. |
Quote:
Quote:
prefix them, e.g., if you want to them in ~/mymovies: Code:
for f in *.avi preview the actions before actually implementing) |
Thanks for your help. I add nearly fixed all I needed. I did not gain that much time but I know more things now.
Unfortunately I could not fix the following script to destroy empty with space or special characters inside: Code:
find ./ -type d -exec rmdir 2>/dev/null {} \; |
The '-depth' option to find will do what you want with removing empty directory trees.
Also: Read that manpage! |
GNU find also has a -delete option, so you don't have to call on the external rmdir. It automatically implies -depth when dealing with directories.
|
All times are GMT -5. The time now is 06:13 PM. |