Ritesh
Ritesh

Reputation: 75

Renaming files in unix of particular date

for filename in $*; do mv "$filename" "${filename//.temp/}";

I am using this script to rename files.
It removes .temp from filename. I have saved this as a.sh script and execute it with the files I want to rename. But this becomes tedious if I want to rename say 100 files and only of particular date. is there a way I pass only date with the script as a parameter and it renames files of that date only without having me to pass the filenames

Upvotes: 1

Views: 330

Answers (2)

Rahul Verma
Rahul Verma

Reputation: 3079

You can modify a.sh like this :

find . -maxdepth 1 -type f -iname "*.temp" -exec bash -c 'mv $0 ${0//.temp/}' {} \;

newermt is for the date the file is modified last time.

and can execute it as

./a.sh 2017-09-27

To test if this command works on command line, you can try this outside your script:

find . -maxdepth 1 -type f -iname "*.temp" -exec bash -c 'mv $0 ${0//.temp/}' {} \;

Upvotes: 1

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

another option is to use -nt test

# create a timestamp file
timestamp_filename=/tmp/$$
touch -t 201709270000 "$timestamp_filename"
trap 'rm "$timestamp_filename"' EXIT

# then to check a file is newer
if [[ "$a_file" -nt "$timestamp_filename" ]]; then
# a_file is newer
fi

Upvotes: 0

Related Questions