Hamham
Hamham

Reputation: 33

Is there any way to keep name of file(+ date hour)

I want to keep the output of this command as it is but I don't want the path, only the file name.

ex. 04-06-2020 23:09:02 filename not 04-06-2020 23:09:02 /root/dir1/dir2/filename

This is the command:

find path -type f -printf '%TY-%Tm-%TT %p\n' | sort -r

Upvotes: 1

Views: 56

Answers (2)

Firstly it is better set the date as $ date +"%F %T" so that you can sort it in time order.

The solution is to apply the command basename file to each file in a list. Like this. Create a file called shorts and put it in ~/bin/ directory. $ chmod u+x ~/bin/shorts The file contains bash code:-

#!/bin/bash
[[ -z "$1" ]] && echo "usage: shorts file_with_date_file_list.txt" && exit 9
declare -a f=()
while read data 
do 
    f=( $data )
    f[2]=$(basename ${f[2]})
    echo ${f[@]}
done < $1

Now the f[2] is because you have 3 columns if you have only 2 change the 2s to 1s.

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 361977

Change %p to %f.

find path -type f -printf '%TY-%Tm-%TT %f\n' | sort -r

%f File's name with any leading directories removed (only the last element).
%p File's name.

Source

Upvotes: 3

Related Questions