LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-27-2005, 11:59 PM   #1
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,427

Rep: Reputation: 53
Command line to sort files


Hi folks,

I have following files inside a directory, e.g.;
/parent_directory/
package-01
package-02
package-03
etc.
file-01.patch
file-02.patch
file-03.patch
etc.

What single command line shall I use to send all .patches to a new sub_directory
/parent_directory/patches/

and all packages to a new sub_directory
/parent_directory/packages/

respectively. Both sub_directories have to be created in executing the command line.

TIA

B.R.
satimis

Last edited by satimis; 06-28-2005 at 12:01 AM.
 
Old 06-28-2005, 12:31 AM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Perhaps I'm missing the point, but...
Code:
chdir /parent_directory; mkdir patches; mkdir packages; mv *patch patches; mv package-* packages
?? It's five commands, but still only one command line. Once the directories exist, you will get error messages, but you can suppress those like this:
Code:
chdir /parent_directory; mkdir patches 2>/dev/null; mkdir packages 2>/dev/null; mv *patch patches; mv package-* packages

Last edited by CroMagnon; 06-28-2005 at 12:37 AM.
 
Old 06-28-2005, 12:56 AM   #3
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,427

Original Poster
Rep: Reputation: 53
Hi CroMagnon,

Tks for your advice.

Quote:
Code:
chdir /parent_directory; mkdir patches; mkdir packages; mv *patch patches; mv package-* packages
For .patch file there will be no problem. But for "packages file" they have variable name without a fixed patent.

Can I apply follwing command line so that all .patch files will be moved first to /parent_directory/patches/ and then all remaining files moved to /parent_directory/packages/ afterwards

Code:
chdir /parent_directory; mkdir patches; mkdir packages; mv *patch patches && mv * packages
B.R.
satimis
 
Old 06-28-2005, 01:12 AM   #4
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Yes, you can do that. Of course, if there are any files in that directory that aren't patches or packages, they will also be moved to the packages dir.
Also, if there are two or three patterns to the package names, you could make use of that like this (as the last command):
Code:
mv *tar.gz *tar.bz2 *tgz packages
 
Old 06-28-2005, 01:26 AM   #5
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,427

Original Poster
Rep: Reputation: 53
Hi CroMagnon,

Quote:
Code:
mv *tar.gz *tar.bz2 *tgz packages
Sorry, I over-looked and forgot to mention that on the remaining files there are read files such as;

udev-config-1.rules
udev-config-2.permissions
etc.

I expect to move all of them to ~/packages directory

B.R.
satimis
 
Old 06-28-2005, 02:22 AM   #6
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,578

Rep: Reputation: 127Reputation: 127
You could also use find like :
Code:
[ ! -d "../packages" ] && mkdir packages
[ ! -d "../patches" ] && mkdir patches
find . -name "*.patch" -exec mv {} ../patches/ \;
find . -type f -exec mv {} ../packages/ \;
Naturally, all remaining file will be moved in
../packages in this case

Last edited by keefaz; 06-28-2005 at 02:24 AM.
 
Old 06-28-2005, 02:50 AM   #7
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,427

Original Poster
Rep: Reputation: 53
Hi keefaz,

Tks for your advice.

Whether you suggest to write a script or to run following command lines separately after "cd /parent_directory"

Code:
[ ! -d "../packages" ] && mkdir packages
[ ! -d "../patches" ] && mkdir patches
find . -name "*.patch" -exec mv {} ../patches/ \;
find . -type f -exec mv {} ../packages/ \;
B.R.
satimis
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding files through command line browser Linux - General 6 05-21-2005 06:53 PM
Moving files from the command line adam-red Linux - Newbie 5 12-06-2004 04:42 AM
rename files on a command line? CrazyPilot Linux - General 9 06-30-2004 04:10 PM
How do I edit files in command line sweb74 Linux - Newbie 13 01-27-2004 10:53 PM
Editing files at command line Goatdemon Linux - Newbie 15 08-12-2002 11:56 AM


All times are GMT -5. The time now is 01:19 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration