LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 11-19-2010, 07:19 AM   #1
loopingz
LQ Newbie
 
Registered: Nov 2010
Location: France
Distribution: Ubuntu 10.10
Posts: 6

Rep: Reputation: 0
Question 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.
 
Old 11-19-2010, 07:28 AM   #2
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by loopingz View Post
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?
 
Old 11-19-2010, 07:33 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 11-19-2010, 08:09 AM   #4
loopingz
LQ Newbie
 
Registered: Nov 2010
Location: France
Distribution: Ubuntu 10.10
Posts: 6

Original Poster
Rep: Reputation: 0
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.

Last edited by loopingz; 11-19-2010 at 11:20 AM.
 
Old 11-20-2010, 12:18 AM   #5
adityavpratap
Member
 
Registered: Dec 2004
Location: Hyderabad, India
Distribution: Slackware 13, Ubuntu 12.04
Posts: 440

Rep: Reputation: 32
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.
 
Old 11-20-2010, 05:37 AM   #6
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by loopingz View Post
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
 
Old 11-20-2010, 08:14 AM   #7
loopingz
LQ Newbie
 
Registered: Nov 2010
Location: France
Distribution: Ubuntu 10.10
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 11-20-2010, 09:17 AM   #8
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by loopingz View Post
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 View Post
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)
 
Old 11-23-2010, 04:19 PM   #9
loopingz
LQ Newbie
 
Registered: Nov 2010
Location: France
Distribution: Ubuntu 10.10
Posts: 6

Original Poster
Rep: Reputation: 0
Thumbs up

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 {} \;
 
Old 11-23-2010, 06:48 PM   #10
2ck
Member
 
Registered: Mar 2010
Location: /home/twock
Distribution: Debian
Posts: 74
Blog Entries: 9

Rep: Reputation: 21
The '-depth' option to find will do what you want with removing empty directory trees.
Also: Read that manpage!
 
Old 11-24-2010, 01:37 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mkdir : cannot create directory : no such file or directory patcheezy Linux - Newbie 6 05-13-2009 11:26 AM
Time stamps following move to British Summer Time aikempshall Linux - General 2 03-30-2007 08:30 AM
how to move a directory ? graziano1968 Linux - General 6 11-29-2006 09:55 AM
Need to create a script to move files from one directory to another mustang05 Solaris / OpenSolaris 4 06-16-2006 10:25 AM
write permissions for directory - not accidently move/deleted the directory linuxgamer Linux - Newbie 10 12-02-2003 03:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration