LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to move and create a directory at the same time. (https://www.linuxquestions.org/questions/linux-general-1/how-to-move-and-create-a-directory-at-the-same-time-845296/)

loopingz 11-19-2010 07:19 AM

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.

vonbiber 11-19-2010 07:28 AM

Quote:

Originally Posted by loopingz (Post 4164295)
Hi,

mv *.avi /nonexisting_directory/
nonexisting_directory being the name of the file without .avi, changing for each file.

Code:

for f in *.avi
do
mkdir -p /${f%.*}
mv $f ${f%.*}
done

Do a test first to check that's what you want, e.g.,
Code:

for f in *.avi
do
echo "mkdir -p /${f%.*}"
echo "mv $f ${f%.*}"
done

Does that answer your question?

David the H. 11-19-2010 07:33 AM

Code:

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:

http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

loopingz 11-19-2010 08:09 AM

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.

adityavpratap 11-20-2010 12:18 AM

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.

vonbiber 11-20-2010 05:37 AM

Quote:

Originally Posted by loopingz (Post 4164344)
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


loopingz 11-20-2010 08:14 AM

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.

vonbiber 11-20-2010 09:17 AM

Quote:

Originally Posted by loopingz (Post 4165312)
Correct directory are created but they go into root folder /. Which I will have to clean carefully.

That's what you requested in your first post
Quote:

Originally Posted by loopingz (Post 4165312)
mv *.avi /nonexisting_directory/

If you want the directories to be created somewhere else you have to
prefix them, e.g., if you want to them in ~/mymovies:
Code:

for f in *.avi
do
mkdir -p "~/mymovies/${f%.*}"
mv "$f" "~/mymovies/${f%.*}"
done

Also I advised you to run a test first (with echo, to
preview the actions before actually implementing)

loopingz 11-23-2010 04:19 PM

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 {} \;

2ck 11-23-2010 06:48 PM

The '-depth' option to find will do what you want with removing empty directory trees.
Also: Read that manpage!

David the H. 11-24-2010 01:37 AM

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.