Get latest file creation time in Unix [duplicate] - linux

This question already has answers here:
Bash function to find newest file matching pattern
(9 answers)
Closed 7 years ago.
I've two files FileA and FileB. Can someone please let me know how to get time for latest created file in a folder in Unix?

Both for only two files and the general case of n files, you can use find:
find -type f -printf '%T# \n' | sort -n | tail -1
If the files need to match a pattern, you can use something like:
find -type f -name 'example*.txt' -printf '%T# \n' | sort -n | tail -1
This prints all modification times of files in the working directory, sorts them, then selects the last (largest) one.

Related

Listing only name and size of the file with largest size (UNIX) [duplicate]

This question already has answers here:
How to find the largest file in a directory and its subdirectories?
(17 answers)
Closed last year.
How can I only list the name and the size of the largest file size in a directory I used this command but It didn't work when I tried on a different directories.
find . -type f -exec ls -lS {} \; | sort -t" " -n -k5 | cut -d" " -f5,10 | tail -n1
This should work
find . -maxdepth 1 -printf '%s %p\n'|sort -nr|head -n 1
the number 1 after head -n means how many of the largest files it'll output and if you want to find files in sub-directories as well you can do that by removing the -maxdepth 1 or changing the number to a larger one.
for more info see the replies in this earlier post: How to find the largest file in a directory and its subdirectories?
For posts about this kind of stuff i suggest tagging them as bash, sh or shell.

Archiving Shell Script [duplicate]

This question already has answers here:
Find and xargs to correctly handle filenames with spaces in their names
(3 answers)
Closed 3 years ago.
I'm very new to shell script and I'm having issues with archiving files.
I have a folder with .xlsm files and I want those files that passed the retention period. I was able to archive except I'm having issues with those files having spaces with their filename eg. X y z.xlsm. below is my sample code.
find ${work_dir} -type f -mtime +${retention} | xargs tar -cvf ${Destination}/archive.tar
Any idea how to achieve it?
Thanks!
Use NUL as delimiter (-print0 for find, and --null for xargs).
find ${work_dir} -type f -mtime +${retention} -print0 | xargs --null tar -cvf ${Destination}/archive.tar

Search recursively all files with a given name replacing a word [duplicate]

This question already has answers here:
BASH: recursive program to replace text in a tree of files
(3 answers)
Closed 5 years ago.
From a given folder, I want to search recursively accross all subfolders searching for files with name file.txt replacing all occurences of Foo -case sensitive-
with Bar.
Which is the simplest way of achieving this with basic scripting (Bash / sed / grep / find...).
find + sed solution:
find . -type f -name "file.txt" -exec sed -i 's/Foo/Bar/g' {} \;

Bash - Find largest file and print its path [duplicate]

This question already has answers here:
How to find the largest file in a directory and its subdirectories?
(17 answers)
Closed 5 years ago.
I need to find the largest file of chosen directory and sub-directories and print the path of that file
I can find the biggest file (I think so)
find . -type f | wc -l
However, I'm stuck on printing the path
find . -type f | xargs ls -l | sort -nk 5 | tail -n1 | awk '{print $NF}'
What i would do :
find . -type f -exec du {} \; | sort -n | awk 'END{$1=""; print}'

viewing the largest file in the directory using linux? [duplicate]

This question already has answers here:
How to find the largest file in a directory and its subdirectories?
(17 answers)
Closed 7 years ago.
1.How to view the largest file in the directory using linux commands.
2.As i followed the following command ls -lh.
3.Is any other way to use the linux commands to view the largest file inside the directory with its size in human readable format.
Try:
$ find . -maxdepth 1 -printf '%s %p\n'|sort -nr|head
It will give you the top 10 in your directory. And if you just want the largest one:
$ find . -type f | xargs ls -1S | head -n 1
With its "parameters/attributes" (size, permissions, creation date & time):
$ find . -type f | xargs ls -lS | head -n 1
And if youy want to use ls without find try:
$ ls -S . | head -1
ls -Slh | tail +2 | head -1
which uses ls to list your files in size order, long format with human readable sizes. tail +2 removes the first line of your output which is a total size and head gives you the largest file.
I solved the question using this command:
ls -Slh | head -2
This lists by size and selects the first two results.

Resources