How to move and create a directory at the same time.
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
for file in *.avi; do
mkdir ${file%.avi}
mv -n -t ${file%.avi} $file
done
This will loop through all the .avi files in the directory, create a subdirectory for each one, then move the file into it. The bracketed variables use parameter substitution to strip off the file ending. Using the -n and the -t pattern in mv ensures that no files will be overwritten if something goes wrong with the directory operation. You may want to consider using the -i "interactive" option as well.
To learn the basics of scripting, check out these links:
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 {} \;
edit:
This one cleaned empty dir:
Code:
find ./ -type d -exec rmdir 2>/dev/null {} \;
But I still have empty dirs with empty dirs inside.
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.
Thank you guys.
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.
You need to use quotes in that case:
Code:
for f in *.avi
do
mkdir -p "/${f%.*}"
mv "$f" "${f%.*}"
done
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.