LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 07-22-2008, 07:58 AM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
Need some help with a basic shell script


Could somebody show me how to write a small shell script to organize some files.

Basically, I have a parent directory with about 3 sub directories. There are txt files scattered all throughout. I want to write a script that searches the parent directory along with the subdirectories for txt files and then moves them to the parent directory. Or better yet, would it be better to have the program search for txt files, then as soon as it finds one it moves it directly to the parent directory then resumes the search.
 
Old 07-22-2008, 08:22 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
for file in `find . -name *.txt` ; do mv $file <path> ; done

<path> is the absolute path of the destination folder, or the relative path from where you run the command.

Note the backtics (not single-quotes)
 
Old 07-22-2008, 08:29 AM   #3
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Code:
DIR=/path/to/files
find $DIR -mindepth 2 -name \*.txt -exec mv '{}' $DIR \;

Last edited by jcookeman; 07-22-2008 at 08:36 AM.
 
Old 07-22-2008, 08:38 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
-name \*.txt
Why the backslash? I get the same result with or without it.
 
Old 07-22-2008, 08:40 AM   #5
Pearlseattle
Member
 
Registered: Aug 2007
Location: Zurich, Switzerland
Distribution: Gentoo
Posts: 999

Rep: Reputation: 142Reputation: 142
I don't have right now a *nix-terminal available, so please tell me if I am wrong:
First of all the "find" command searches for files.
E.g. if you want to search starting from the directory "/mydir" and navigate through all its subdirectories you would write:

find /mydir -name "*.txt"

Now, if you want to move all files you find to some other directory "/otherdir" you would write something like:

find /mydir -name "*.txt" -exec mv '{}' /otherdir \;

Something like this?
Greetings.
 
Old 07-22-2008, 08:49 AM   #6
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Quote:
Originally Posted by Pearlseattle View Post
First of all the "find" command searches for files.
E.g. if you want to search starting from the directory "/mydir" and navigate through all its subdirectories you would write:

find /mydir -name "*.txt"

Now, if you want to move all files you find to some other directory "/otherdir" you would write something like:

find /mydir -name "*.txt" -exec mv '{}' /otherdir \;

Something like this?
Greetings.
Yes you got it.

FYI: Escaping * (\*) can be necessary in other shells and depending on what shell options are enabled.
 
Old 07-22-2008, 09:22 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
for file in `find . -name *.txt` ; do mv $file <path> ; done
take note of spaces in file names.
 
  


Reply

Tags
exec, find



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
Very basic shell script question..... redhatpenguin Programming 2 09-09-2007 07:42 PM
Basic Shell Script Help Vasquez Linux - Newbie 3 02-28-2005 07:14 PM
basic shell script help lin00b Linux - Newbie 2 10-08-2004 11:32 PM
Basic shell script, please help colly Linux - General 4 10-05-2004 11:24 AM
basic shell script help coyote399 Linux - Newbie 6 04-14-2004 09:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:47 AM.

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