LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-23-2011, 12:44 PM   #1
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Rep: Reputation: 10
How to store files/directories in different different directories!!


Hi legends,

I am writing a script, in that my requirement is, if all the fill types stored in one directory from that we need to separate different different directories based on the file types.

for example in a directory(anish). 5 different types files
1- directory
2- .txt files
2- .sh files

like that and my requirement is the (1- directory is moved to one new directory(dir) which we are given in the script)

and (2 .txt files are moved to another new directory(test) which we are given in the script)

and ( 2 .sh files are moved to another new directory(bash) which we are given in the scrip)

finally the directory anish should be empty..using bash script.how it is possible !!

for this process i need to maintain log also , please guide me !!!
 
Old 03-23-2011, 12:50 PM   #2
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 215

Rep: Reputation: 66
I am not good at scripting, but this should work. I am sure someone else can do it more efficiently.


#/bin/bash
LOG=/var/log/scriptlog
for i in `ls /path/to/anish`
do
if [ -d $i ]
then
echo "$i is a directory" >> $LOG
mv -v $i /whereever/you/want/it/moved >> $LOG
fi
if [ "$i" == "*.txt" ]
then
echo "$i is a text file" >> $LOG
mv -v $i /whereever/you/want/it/moved >> $LOG
fi
if [ "$i" == "$.sh" ]
then
echo "$i is a shell script" >> $LOG
mv -v $i /whereever/you/want/it/moved >> $LOG
fi
done
 
Old 03-23-2011, 02:49 PM   #3
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Code:
LOG=/var/log/scriptlog
cd /path/to/anish
date >> $LOG
echo "" >> $LOG
echo "The listing before the operation was :" >> $LOG
echo "/path/to/anish" >> $LOG
ls -l >> $LOG
mv *.txt /path/to/text
mv *.sh /path/to/bash
mv -f * /path/to/other_directory
echo "" >> $LOG
echo "The listing after the operation was :" >> $LOG
ls -l >> $LOG
echo "" >> $LOG
Savona, your script works if it is run from the anish directory, but from elsewhere ?

Last edited by smoker; 03-23-2011 at 03:12 PM. Reason: missed a bit; and had a typo in mv *
 
Old 03-23-2011, 03:50 PM   #4
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 215

Rep: Reputation: 66
Quote:
Originally Posted by smoker View Post
Savona, your script works if it is run from the anish directory, but from elsewhere ?
I know I am not much of a script writer, but can you explain why it wouldn't work from outside the anish directory? I am using full paths for everything. I am trying to learn myself, so please do not take this as sarcasm.
 
Old 03-23-2011, 04:38 PM   #5
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Quote:
Originally Posted by savona View Post
I know I am not much of a script writer, but can you explain why it wouldn't work from outside the anish directory? I am using full paths for everything. I am trying to learn myself, so please do not take this as sarcasm.
Where are you using the full path in the mv commands ?

i does not contain the path, only the filename or directory name. So if you run it from /home instead of /home/anish the script will not find the file to move it.

Have you tested your script ?

Last edited by smoker; 03-23-2011 at 04:42 PM.
 
Old 03-23-2011, 04:52 PM   #6
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Here's a more long winded version, which also deals with non- txt or sh files separately from directories and reports more fully. Most of the code is done for the benefit of the log.

Code:
#/bin/bash
MYPATH=/home/smoker
LOG=/home/smoker/test.log

cd $MYPATH/anish
date >> $LOG
echo "" >> $LOG
echo "The listing before the operation was :" >> $LOG
echo "$MYPATH/anish" >> $LOG # could also be pwd >> $LOG
ls -l >> $LOG
echo "" >> $LOG
echo "Moving ..." >> $LOG
mv -v *.txt $MYPATH/text >> $LOG
mv -v *.sh $MYPATH/bash >> $LOG
mv -v *.* $MYPATH/unknown >> $LOG
mv -vf * $MYPATH/holding >> $LOG
echo "...finished." >> $LOG
echo "" >> $LOG
echo "The listing after the operation was :" >> $LOG
echo "$MYPATH/anish" >> $LOG
ls -l >> $LOG
echo "" >> $LOG
echo "$MYPATH/text" >> $LOG
ls -l $MYPATH/text >> $LOG
echo "" >> $LOG
echo "$MYPATH/bash" >> $LOG
ls -l $MYPATH/bash >> $LOG
echo "" >> $LOG
echo "$MYPATH/holding" >> $LOG
ls -l $MYPATH/holding >> $LOG
echo "" >> $LOG
echo "$MYPATH/unknown" >> $LOG
ls -l $MYPATH/unknown >> $LOG
echo "" >> $LOG
Which gives output like
Quote:
Wed Mar 23 21:26:44 GMT 2011

The listing before the operation was :
/home/smoker/anish
total 8
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:09 1.sh
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:08 1.txt
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:09 2.sh
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:08 2.txt
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:09 3.sh
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:08 3.txt
-rw-rw-r-- 1 smoker smoker 0 Mar 23 21:10 69.dat
drwxrwxr-x 2 smoker smoker 4096 Mar 23 20:09 test1
drwxrwxr-x 2 smoker smoker 4096 Mar 23 20:09 test2

Moving ...
`1.txt' -> `/home/smoker/text/1.txt'
`2.txt' -> `/home/smoker/text/2.txt'
`3.txt' -> `/home/smoker/text/3.txt'
`1.sh' -> `/home/smoker/bash/1.sh'
`2.sh' -> `/home/smoker/bash/2.sh'
`3.sh' -> `/home/smoker/bash/3.sh'
`69.dat' -> `/home/smoker/unknown/69.dat'
`test1' -> `/home/smoker/holding/test1'
`test2' -> `/home/smoker/holding/test2'
...finished.

The listing after the operation was :
/home/smoker/anish
total 0

/home/smoker/text
total 0
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:08 1.txt
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:08 2.txt
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:08 3.txt

/home/smoker/bash
total 0
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:09 1.sh
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:09 2.sh
-rw-rw-r-- 1 smoker smoker 0 Mar 23 20:09 3.sh

/home/smoker/holding
total 8
drwxrwxr-x 2 smoker smoker 4096 Mar 23 20:09 test1
drwxrwxr-x 2 smoker smoker 4096 Mar 23 20:09 test2

/home/smoker/unknown
total 0
-rw-rw-r-- 1 smoker smoker 0 Mar 23 21:10 69.dat
Also, if you were to replace anish with $1 everywhere it appears above, then you could run the script on any directory under the defined path, by calling the script with the required directory name.
i.e.

Code:
./test.sh anish

Last edited by smoker; 03-23-2011 at 05:08 PM.
 
Old 03-23-2011, 04:54 PM   #7
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 215

Rep: Reputation: 66
Quote:
Originally Posted by smoker View Post
Where are you using the full path in the mv commands ?

i does not contain the path, only the filename or directory name. So if you run it from /home instead of /home/anish the script will not find the file to move it.

Have you tested your script ?
No need to get all worked up, I am trying to learn like everyone else. And yes, you are VERY correct, it would not work.
 
Old 03-23-2011, 05:26 PM   #8
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Who's worked up ?
Unless you test your code, and more importantly know _where_ it is going to be working, you can end up with some bad results.
Imagine this was a script to delete text files in a certain directory. You may end up deleting files in the working directory after listing files elsewhere, if they have the same name. Even worse if the command is to delete ALL text files in that directory.

Last edited by smoker; 03-23-2011 at 05:27 PM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
syslog to central server and store logs in separate host directories steve.goldner Linux - Enterprise 11 04-07-2012 02:26 PM
Copying files and sub-directories of a directory except the directories named ".abc" sri1025 Linux - General 2 08-24-2010 08:53 AM
How do I copy over directories to directories with the same name without overwriting? SentralOrigin Linux - General 1 03-14-2009 01:09 AM
CHMOD directories.sub-directories.files zerojosh Linux - Software 2 11-19-2005 03:22 PM
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 12:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:14 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