How to store files/directories in different different directories!!
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 !!!
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
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 *
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.
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.
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.