Move Multiple Files adding date timestamp before file type

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Move Multiple Files adding date timestamp before file type
# 1  
Old 05-23-2013
Move Multiple Files adding date timestamp before file type

There are files in a directory and I have to move multiple files adding datetimestamp before the file type.



I have to move this files to archive directory adding datatimestamp before .csv


# 2  
Old 05-23-2013
Works with GNU date on modern linux bash:
#! /bin/bash

dt=$(date +%Y%m%d%H%M%S)
for file in /Data/*.csv
    mv /Data/$file /archive/${name}_${dt}.csv

# 3  
Old 05-23-2013
is there any sed or awk command that does this logic. The reason being I work on an ETL tool which mainly support one line logic otherwise I have to maintain a shell script beyond the tool.
# 4  
Old 05-23-2013
Originally Posted by balajesuri
Works with GNU date on modern linux bash:
#! /bin/bash

dt=$(date +%Y%m%d%H%M%S)
for file in /Data/*.csv
    mv /Data/$file /archive/${name}_${dt}.csv

Not quite. The first time through this loop with the sample data specified by the OP would try to move /Data/Data/abc.csv to /archive/Data/abc_timestamp.csv.

I would tend to parameterize it a little bit more with something like:
if [ "${destdir:0:1}" != "/" ]
then    destdir="$PWD/$destdir"
if [ ! -d "$srcdir" ] || [ ! -d "$destdir" ]
then    printf "Source (%s) and target(%s) directories must exist.\n" \
                "$srcdir" "$destdir" >&2
        printf "Usage: %s source_directory target_directory\n" "${0##*/}" >&2
        exit 1
cd "$srcdir"
ts=$(date +%Y%m%d%H%M%S)
for file in *.csv
do      mv "$file" "$destdir/${file%.*}_$ts.csv"

I usually use the Korn shell, but this will work with recent versions of ksh and bash without change. It defaults to /Data and /archive as the source and target directories but they can be overridden by supplying (absolute or relative) directory names as command line arguments. It also protects itself in case any of the components of the source and target files contain whitespace characters by quoting user supplied data. Production code should also verify that the cd succeeds and that all of the mv commands succeed.

But, now that I see that the OP has changed the requirements, this is obviously overkill. You could create a shell script using sed and write it as a 1-liner, write an awk script that invokes the shell to get the date and perform the moves, or convert a corrected shell script to a 1-liner but I won't help you play that game. I will always go for a readable, more easily maintainable script than a hard to read 1-liner.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-23-2013
I don't think you can dream up a sed or awk one liner as you need file system interaction to move the files; perl might do. Did you consider the rename perl script?
# 6  
Old 05-23-2013
No, I am not so good at unix scripting.

Can one line works with moving multiple files adding date at the end?


# 7  
Old 05-23-2013
You can do most things with a one line shell script that you can do with a 500 line shell script except edit it easily, understand it, debug it, and make changes to it that are likely to work the first time (or ever).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find file type recursively and move

Hello, I supposed that it was working fine but now I see that it's not working as expected. I am running under ubuntu14.04, trusty. My plan was to search folderA and all subdirectories and move any txt file to destination folder, folderB : find /home/user/folderA/ -type f -iname "*.txt"... (0 Replies)
Discussion started by: baris35
0 Replies

2. Shell Programming and Scripting

Move multiple files 4rm Source to different target folders based on a series num in the file content

Dear Experts my scenario is as follows... I have one source folder "Source" and 2 target folders "Target_123456" & "Target_789101". I have 2 series of files. 123456 series and 789101 series. Each series has got 3 types of fiels "Debit", "Refund", "Claims". All files are getting... (17 Replies)
Discussion started by: phani333
17 Replies

3. Shell Programming and Scripting

Sort and move multiple files by modification date

Hi I have a problem, I have a large group of archive files in a folder some are later versions of the same archive, the only difference btween them is that the archiving program we use appends the name with a code for it to keep track of in its data base, and the modification date. I am starting... (6 Replies)
Discussion started by: Paul Walker
6 Replies

4. Shell Programming and Scripting

Adding filename and line number from multiple files to final file

Hi all, I have 20 files (file001.txt upto file020.txt) and I want to read them from 3rd line upto end of file (line 1002). But in the final file they should appear to start from line 1. I need following kind of output in a single file: Filename Line number 2ndcolumn 4thcolumn I... (14 Replies)
Discussion started by: bioinfo
14 Replies

5. UNIX for Dummies Questions & Answers

Move Command with Adding Yesterday date

How to move a file with mv command by adding yesteday's date(YYYYMMDD) at the end Example: /a/abc.txt should be moved to the same folder /a/abc_yyyymmdd.txt YYYYMMDD should be yesterday's date (2 Replies)
Discussion started by: eskay
2 Replies

6. Shell Programming and Scripting

Check if a date field has date or timestamp or date&timestamp

Hi, In a field, I should receive the date with time stamp in a particular field. But sometimes the vendor sends just the date or the timestamp or correctl the date&timestamp. I have to figure out the the data is a date or time stamp or date&timestamp. If it is date then append "<space>00:00:00"... (1 Reply)
Discussion started by: machomaddy
1 Replies

7. Shell Programming and Scripting

write to multiple files depending on file type (solved)

I want a script that will add comments to a file before check-in files. comments depend on type of files. i have a script to list files in the directory that will be checked-in There are xml, js, html, vm files. vm will use comments like c/c++ ( // or /*..*/) can you help me add a comment line... (0 Replies)
Discussion started by: pradeepmacha
0 Replies

8. Shell Programming and Scripting

Adding timestamp to all inbound files

hi, can someone give me a sample shell scripts to make all inbound files will a have a Moveit timestamp.I think MoveIt is a server,thanks. (5 Replies)
Discussion started by: sonja
5 Replies

9. UNIX for Dummies Questions & Answers

UNIX command: mv - Objective: move files based on timestamp

I was wondering if there was a command to move files from one directory to another subdirectory based on the timestamp, i.e. moving from directory A files that have a timestamp of before the year 2005 into directory B. Directory B is a subdirectory located in directory A. I was advised to... (4 Replies)
Discussion started by: HLee1981
4 Replies

10. UNIX for Dummies Questions & Answers

move file change timestamp

Hello, Alright solution: I need to move files to a backup folder, changing the datestamp to the current day so that the file stays in the backup folder for the full 30 days before another script removes it. Obviously, any file I move in will preserve the timestamp which is what I *don't* want.... (2 Replies)
Discussion started by: tekster757
2 Replies
Login or Register to Ask a Question

Featured Tech Videos