display the most recent file in a directory using linux shell - linux

I need to display the content of the recent file in a directory, for my example it's a log file that gets generated for each execution hence I need to display the newest one
the command :
ls -Art | tail -n 1
output the right name, meaning the most recent file in a directory, my goal is to output the content of this file by further piping
How could we do this?

cat `ls -Art | tail -n1`
or,
ls -Art | tail -n 1 | xargs cat

Related

Execute a random script in a folder

I need to run a random script in a folder. I can use 'ls -1 /home/sepinto/EML-Samples/scripts/regular | sort -R | head -1' to get a random file name but how can I execute it?
Thanks
I can use 'ls -1 /home/sepinto/EML-Samples/scripts/regular | sort -R | head -1' to get a random file name but how can I execute it?
First, change your ls command to list the files with complete path, then just execute them either directly with backticks or storing them in a variable.
SCRIPTNAME=`ls -1 /home/sepinto/EML-Samples/scripts/regular/*.sh | sort -R | head -1`
echo "Executing $SCRIPTNAME"
"$SCRIPTNAME"
The above assumes that all your files have a .sh ending, change the ls argument if this is not the case.
It depends on the shell you are using.
I will assume your shell is bash or sh and the scripts are already executable.
Then all you need to do is enclose your command line above in backticks.
Everything you type between backticks is evaluated (executed) by the shell and then replaced by the command's output.
`ls -1 /home/sepinto/EML-Samples/scripts/regular | sort -R | head -1`
Edit:
Turns out that the path is stripped if ls -1 is piped into another command.
A solution is to store the path in a variable so you can use it twice:
d="/home/sepinto/EML-Samples/scripts/regular"; $d/`ls -1 $d/ | sort -R | head -1`

Trying to diff two files with generated paths - no such file or directory - but files exist

I want to diff two files in the same directory in a bash script. To get the full paths of these two files (I need this because the script isn't running the same directory), I did:
pathToOld=$(ls -Art /dir/path/here | grep somestring | tail -n2 | head -n1)
pathToOld="/dir/path/here/${pathToOld}"
and
pathToNew=$(ls -Art /dir/path/here | grep somestring | tail -n 1)
pathToNew="/dir/path/here/${pathToNew}"
I was able to figure out the above from the following links: link1, link2, link3
If I echo these path in the .sh script, it comes out correctly, like:
>echo "${pathToOld}"
/dir/path/here/oldFile
But when I try to diff the files like so:
diff pathToOld pathToNew
It tells me:
diff: pathToOld: No such file or directory
diff: pathToNew: No such file or directory
How do I make this work?
Btw, I have also tried to pipe sed -z 's/\n/ /g' (inspired by this) to both lines but that hasn't helped.

why this command not working?(pipe, redirection)

bash-3.2$ ls | grep Makefile > a.txt | cat a.txt
why this don't work?? "Makefile" is exist.
There is no output from the grep command since you are redirecting it to a file. Therefore the pipe gets closed before the cat a.txt actually gets called. As per my comment, use && instead of that last |.
Do do what you want which is to save the output of a command to a file but still print to stdout use tee. (also I like to do ls -1 to make sure only one item per line is printed this is not necessary but force of habit)
ls -1 | grep Makefile | tee a.txt

Linux commands to get Latest file depending on file name

I am new to linux. I have a folder with many files in it and i need to get the latest file depending on the file name. Example: I have 3 files RAT_20190111.txt RAT_20190212.txt RAT_20190321.txt . I need a linux command to move the latest file here RAT20190321.txt to a specific directory.
If file pattern remains the same then you can try below command :
mv $(ls RAT*|sort -r|head -1) /path/to/directory/
As pointed out by #wwn, there is no need to use sort, Since the files are lexicographically sortable ls should do the job already of sorting them so the command will become :
mv $(ls RAT*|tail -1) /path/to/directory
The following command works.
ls | grep -v '/$' |sort | tail -n 1 | xargs -d '\n' -r mv -- /path/to/directory
The command first splits output of ls with newline. Then sorts it, takes the last file and then it moves this to the required directory.
Hope it helps.
Use the below command
cp ls |tail -n 1 /data...

How to get the second latest file in a folder in Linux

Found several posts like this one to tell how to find the latest file inside of a folder.
My question is one step forward, how to find the second latest file inside the same folder? The purpose is that I am looking for a way to diff the latest log with a previous log so as to know what have been changed. The log was generated in a daily basis.
Building on the linked solutions, you can just make tail keep the last two files, and then pass the result through head to keep the first one of those:
ls -Art | tail -n 2 | head -n 1
To do diff of the last (lately modified) two files:
ls -t | head -n 2 | xargs diff
Here's a stat-based solution (tested on linux)
for x in ./*;
do
if [[ -f "$x" ]]; then
stat --printf="%n %Y\n" "$x"; fi;
done |
sort -k2,2 -n -r |
sed -n '2{p;q}'
ls -dt {{your file pattern}} | head -n 2 | tail -n 1
Will provide second latest file in the pattern you search.
Here's the command returns you latest second file in the folder
ls -lt | tail -n 1 | head -n 2
enjoy...!

Resources