Need BASH Script Help to Move Files While Creating Directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need BASH Script Help to Move Files While Creating Directories
# 1  
Old 12-10-2016
Need BASH Script Help to Move Files While Creating Directories

I've got this script to loop through all folders and move files that are more than 2 years old. I'm using the install command because it creates the necessary directories on the destination path and then I remove the source. I'd like to change the script to use the mv command since it is much faster than copying the file then deleting it. But I don't know how to get the path without the filename to pass to the mkdir -p command? How should I modify the script?

Code:
for d in *; do

find "$d" -type f -mtime +730 -exec sh -c '

echo "moving $d/{}" &&
install -Dp "/data/Customer Files$d/{}" "/data/Customer Files Over 2 Years Old$d/{}" &&
rm -f "/data/Customer Files$d/{}"' \;

done


Last edited by Don Cragun; 12-10-2016 at 07:07 PM.. Reason: Add CODE tags.
# 2  
Old 12-10-2016
Hi,

Welcome to the Forum!

Here is one way.
Can you try the below and see if it helps:
Code:
#!/bin/bash

SOURCE="/data/Customer-Files-1"
DEST="/data/Customer-Files-Over-2-Years-Old"
PARENTDIR=${SOURCE%/*}
ROOTDIR=${SOURCE##*/}
cd $PARENTDIR

find $ROOTDIR  -type f -mtime +730 | while read line
do
GETSUBDIRNAME=$(dirname $line)
mkdir -p $DEST/$GETSUBDIRNAME 
echo "cp $line to $DEST/$GETSUBDIRNAME"
cp $line $DEST/$GETSUBDIRNAME
# rm -f $SOURCE
done

Make sure to run with cp command as shown above first and compare the files/directory structure and then you can uncomment rm -f command.
# 3  
Old 12-10-2016
This method still uses a copy. These are large files and therefore the cp takes way longer than mv since mv just changes the file pointer and doesn't actually copy the file. Can you post it with using mv?

Quote:
Originally Posted by greet_sed
Hi,

Welcome to the Forum!

Here is one way.
Can you try the below and see if it helps:
Code:
#!/bin/bash

SOURCE="/data/Customer-Files-1"
DEST="/data/Customer-Files-Over-2-Years-Old"
PARENTDIR=${SOURCE%/*}
ROOTDIR=${SOURCE##*/}
cd $PARENTDIR

find $ROOTDIR  -type f -mtime +730 | while read line
do
GETSUBDIRNAME=$(dirname $line)
mkdir -p $DEST/$GETSUBDIRNAME 
echo "cp $line to $DEST/$GETSUBDIRNAME"
cp $line $DEST/$GETSUBDIRNAME
# rm -f $SOURCE
done

Make sure to run with cp command as shown above first and compare the files/directory structure and then you can uncomment rm -f command.
# 4  
Old 12-10-2016
you can modify the code i posted before and change cp to mv

Please read
Code:
man mv

and see how it differs from cp syntax wise !
# 5  
Old 02-03-2017
Note also that if the source and destination directories are on different filesystems, the mv utility will internally act as though cp and rm (and, possibly, chmod) had been done instead).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Move several files into specific directories with a loop

Hello, I'm a first time poster looking for help in scripting a task in my daily routine. I am new in unix but i am attracted to its use as a mac user. Bear with me... I have several files (20) that I manually drag via the mouse into several named directories over a network. I've used rsync... (14 Replies)
Discussion started by: SonnyClark
14 Replies

2. UNIX for Dummies Questions & Answers

Move multipe files to corresponding directories

Hi, In a parent directory there are several files in the form IDENTIFIER1x IDENTIFIER1.yyy IDENTIFIER1_Z, etc IDENTIFIER2x IDENTIFIER2.yyy IDENTIFIER2_Z, etc IDENTIFIER3x IDENTIFIER3.yyy, IDENTIFIER3_Z, etcIn the same parent directory there are corresponding directories named... (7 Replies)
Discussion started by: spirospap
7 Replies

3. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

4. Programming

Script for creating a directory & move the .tif files in it.

Hi Team, I have thousands of TIF files which are converted from PDF. Below is a sample of it. LH9406_BLANCARAMOS_2012041812103210320001.tif LH9406_BLANCARAMOS_2012041812103210320002.tif LH9406_BLANCARAMOS_2012041812103210320003.tif LH9411_ANGENIAHUTCHINSON_2012041812102510250001.tif... (9 Replies)
Discussion started by: paragnehete
9 Replies

5. Shell Programming and Scripting

Recursively move directories along with files/specific files

I would like to transfer all files ending with .log from /tmp and to /tmp/archive (using find ) The directory structure looks like :- /tmp a.log b.log c.log /abcd d.log e.log When I tried the following command , it movies all the log files... (8 Replies)
Discussion started by: frintocf
8 Replies

6. Shell Programming and Scripting

bash script for testing existence of files/folders and creating if neither exist

Hi, I am new to shell-scripting, and doing a lot of reading. I am having some trouble getting started with a simple testing of scripting. I have been experimenting with if, loops, for, test, etc., but still unsure. I seem to have the hang of it when it comes to creating a single file or... (6 Replies)
Discussion started by: me2
6 Replies

7. Shell Programming and Scripting

Loop to move files in different directories

Hi, I have various log files in different paths. e.g. a/b/c/d/e/server.log a/b/c/d/f/server.log a/b/c/d/g/server.log a/b/c/h/e/server.log a/b/c/h/f/server.log a/b/c/h/g/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log and above these have an archive folder... (6 Replies)
Discussion started by: acc01
6 Replies

8. Shell Programming and Scripting

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (4 Replies)
Discussion started by: Sriranga
4 Replies

9. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

10. Shell Programming and Scripting

Bash and Awk for creating directories and moving files

I have a security system that FTPs the camera files to my machine, however I want to sort the pictures (taken every 30s) into directories by hour. Every picture uses the following file format. yymmddhhmmsstt.jpg (where tt is the milliseconds) I am thinking the for loop is best for file... (11 Replies)
Discussion started by: Kiint
11 Replies
Login or Register to Ask a Question