How to create a log file that simply shows the name of a file and what directory it was moved to.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to create a log file that simply shows the name of a file and what directory it was moved to.
# 1  
Old 07-20-2012
Question How to create a log file that simply shows the name of a file and what directory it was moved to.

Newbie...Thank you for your help.

I am creating my first script that simply generates subdirectories to organize video, music, and text files within those subdirectories from my current working directory.

PROBLEM: I am trying to write a log file that contains, for each file, the original file name, as well as the new file path/name it was moved to. I think it has something to do with redirect operators, but I do not know where to start.

Here is what the sample output should look like, and my current code is listed below that. My current code works but as of now, it only creates the subdirectories and moves the files within those subdirectories.

SAMPLE RUN:
Code:
name@school.edu:~/testsuite> ./organizefiles.sh
Are you sure you want to reorganize your files?
Enter y to continue, anything else to cancel:
y
Organizing files!
Finished organizing files!
Bye!


name@school.edu:~/testsuite> ls -R
.:
movies organizefiles.sh organize.log songs textfiles
./movies:
coolclip.wmv movie1.wmv movie2.mpg myvideo.mov
./songs:
awesomesong.mp3 jazz.mp3 rock.mp3 song1.wma song2.mp3
./textfiles:
file1.txt final.txt grades.txt secretdoc.txt


name@school.edu:~/testsuite> cat organize.log
Starting to organize...
coolclip.wmv --> movies/coolclip.wmv
movie1.wmv --> movies/movie1.wmv
movie2.mpg --> movies/movie2.mpg
myvideo.mov --> movies/myvideo.mov
awesomesong.mp3 --> songs/awesomesong.mp3
jazz.mp3 --> songs/jazz.mp3
rock.mp3 --> songs/rock.mp3
song1.wma --> songs/song1.wma
song2.mp3 --> songs/song2.mp3
file1.txt --> textfiles/file1.txt
final.txt --> textfiles/final.txt
grades.txt --> textfiles/grades.txt
secretdoc.txt --> textfiles/secretdoc.txt
name@school.edu:~/testsuite>


MY CURRENT CODE:

Code:
name@shell:~/coursename/scriptpractice>cat organizefiles.sh
#!/bin/sh
# Script to reorganize files

echo "Are you sure you want to reorganize your files?"
echo "Enter y to continue, anything else to cancel: "

read ans # This will get the user's response into the variable "ans"

case $ans in # Dereferencing ans. In is just syntax.

        # If the user says yes
        "y")
                echo "Organizing files!"
                        mkdir music video text
                        mv *.mp3 *.ogg *.wav ./music
                        mv *.wmv *.mov *.mpg ./video
                        mv *.txt ./text
                # Log file "organize.log" created
                        # for FILENAME in *
                echo "Finished organizing files!"
                echo "Bye!"
                ;;

        # If the user says anything else
        *) # * is a wildcard
                echo "User cancelled the process."

esac # As with the if statement, the case statement ends with case in reverse...esac

Moderator's Comments:
Mod Comment edit by bakunin: this looks like homework/coursework, which is forbidden in the regular boards. Please repost the question in the appropriate part of the forum and use the required template for it.

I close this thread until shown that it is not homework.

Last edited by bakunin; 07-21-2012 at 02:25 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move file from one directory and update the list file once moved.

Dears, I have a listfile contains list of files path. i need to read the line of the listfile mv the file to other directory and update the listfile by deleting the lines of the listfile. #!/bin/bash target=/fstest/INVESTIG/Sadiq/TEST_ARCH while read -r line || ]; do mv $line... (19 Replies)
Discussion started by: sadique.manzar
19 Replies

2. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

3. Red Hat

Create same file name to directory name without dropping it

Hi, Under '/home' directory, there is one file called 'maddy'.Usually there used to be directories under /home directory. # ls -alrt total 132 drwx------ 2 hcladmin sys 4096 May 30 10:54 admin drwxr-xr-x 29 root root 4096 Aug 27 03:54 .. drwx------ 2 v6admin dba ... (3 Replies)
Discussion started by: Maddy123
3 Replies

4. UNIX for Dummies Questions & Answers

How to create a TAR File in a Custom Destination Directory?

I want to create the tarzip file into a destination directory, i am in /var/sftp/home/archive/rquadri directory and i am using below command. However it is creating the file in the /var/sftp/home/archive/rquadri directory itself instead of /tmp, may i please know how do i resolve this. tar -cvzf... (5 Replies)
Discussion started by: Ariean
5 Replies

5. Shell Programming and Scripting

Create a flat file and directory structure

Hi All, is there any work around to generate the file and directory structure like below at left side at Output? and exclude all file except .abc .txt Current Directory structure |-------------files |---------------Share |-----------------dir1 |-----------------dir2... (11 Replies)
Discussion started by: heros
11 Replies

6. UNIX for Dummies Questions & Answers

Need help how to create a file (xml) list all files from directory

I have more than 10K songs in two directories on a hard drive. I would like to create a file list all of files name then change to .xml extension to upload to iPhone so I have a Karaoke list on my iPhone. I need your help to create a file by using command in Linux. Files names: 0001 More... (4 Replies)
Discussion started by: ggcc
4 Replies

7. UNIX for Dummies Questions & Answers

create a file inside a directory

create a file inside a directory in one command like current directory is root i want to create a directory inside root and a file inside that directory is there any command like touch /d/d.txt d directory does not exist (1 Reply)
Discussion started by: abhisheklodha13
1 Replies

8. Shell Programming and Scripting

How to create a directory structure with getting input from a file.

Hi How to create a directory structure with getting input from a file. I have file in that following lines are written. ./activemq-4.1.2/activemq-core-4.1.2.jar ./activemq-4.1.2/backport-util-concurrent-2.1.jar ./camel-1.4.0/apache-camel-1.4.0.jar ./camel-1.4.0/lib/activation-1.1.jar... (12 Replies)
Discussion started by: itsjoy2u
12 Replies

9. UNIX for Dummies Questions & Answers

Need to create .bat file to store log file

Hi guys. Can someone point me to a resource that explains this? Basically these are websphere logs that need to be stored daily, I'm on sunOS 5.8. Each new file stored could have the current time as its filename. The script could be run on a cron which I can set up. I'm just not sure how to write... (0 Replies)
Discussion started by: dirtybrown
0 Replies

10. Shell Programming and Scripting

Create file in each user directory

Hi, Im newbie, I wanna to create file in each user directory, how to make that script, maybe someone can give me example, im confusing coz i have to change form one user directory to other Thank U. (8 Replies)
Discussion started by: cleks
8 Replies
Login or Register to Ask a Question