bala
bala

Reputation: 15

Finding the last updated file in shell, Excluding directory

hie, i am new to shell. on searching in StackOverflow I found a code which finds the last updated file in a directory including subdirectory.

find $path -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2 | tail -n

my requirement is to find the last updated file in the directory but not subdirectory

the directory has files in CSV format and 3-subdirectories, is there any way to modify this existing code? or search using CSV tag so that it only checks the CSV file, not the Subdirectory(subdirectory also contains .csv files).

Thanks

Upvotes: 0

Views: 48

Answers (1)

Lester_wu
Lester_wu

Reputation: 171

Use find command with maxdepth 1 argument, it will not search for subdirectory.
Then you can use -exec to add command which will run for matched files.
Finally use tail -1 to get the latest file name, and basename to extract file name from full path.

Here's an example,

basename $(find $Path -maxdepth 1 -name '*.csv' -exec ls -t {} \;  | tail -1)

Upvotes: 1

Related Questions