i want to write a program for the below logic:

  • Take the names of files in a folder
  • For each file:
    • print file name
    • move the file to another new folder (with the name in dd_mm_yy format)

To get file name from its full path, you can use basename:

$ basename /home/cuonglm/test.pl 

So you can do something like:

for f in *; do
  filename="$(basename -- "$f")"
  # Do sftuff here

Your question is not clear. I assume that you want the destination filename to be the modification time of the file, and you don't care about the file's original name. Please correct me if it is wrong.

Bash script. Usage: ./script.sh original_dir dest_dir

find "$1" -maxdepth 1 -exec bash -c '
echo "$1"
export LANG=C
name=$(stat -c "%y" . | sed -r  "s/..(..)-(..)-(..).*/\3-\2-\1/")
mv -iT $0 '"$1"'/"$name"' "{}" \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.