first two results from ls command - linux

I am using ls -l -t to get a list of files in a directory ordered by time.
I would like to limit the search result to the top 2 files in the list.
Is this possible?
I've tried with grep and I struggled.

You can pipe it into head:
ls -l -t | head -3
Will give you top 3 lines (2 files and the total).
This will just give you the first 2 lines of files, skipping the size line:
ls -l -t | tail -n +2 | head -2
tail strips the first line, then head outputs the next 2 lines.

To avoid dealing with the top output line you can reverse the sort and get the last two lines
ls -ltr | tail -2
This is pretty safe, but depending what you'll do with those two file entries after you find them, you should read Parsing ls on the problems with using ls to get files and file information.

Or you could try just this
ls -1 -t | head -2
The -1 switch skips the title line.

You can use the head command to grab only the first two lines of output:
ls -l -t | head -2

You have to pipe through head.
ls -l -t | head -n 3
will output the two first results.

Try this:
ls -td -- * | head -n 2

Related

linux file based using grep cmd

I have a series of files, for example:
ABC_DDS_20150212_CD.csv
ABC_DDS_20150210_20150212_CD.csv
ABC_DFG_20150212_20150217_CD.csv
I want to apply grep command in Linux so I can extract the first 2 files, but not the 3rd file from ls.
I tried to following:
grep -l "" *20150212* -- exclude *20150212_201502*
You can pipe into grep -v:
grep -l "" *20150212* | grep -v "20150212_201502"
I haven't seen people use grep -l like that though. Using ls seems like a cleaner solution:
ls -l *20150212* | grep -v "20150212_201502"
As u mentioned first two files of ls command
then we can use head as well , just like below
ls -l *your_search_name* | head -2
You can extract your matches by using the below command
grep -l "" *20150212*
output
ABC_DDS_20150210_20150212_CD.csv
ABC_DDS_20150212_CD.csv
ABC_DFG_20150212_20150217_CD.csv
and then to get first 2 lines, you can use head command
grep -l "" *20150212* | head -2
Output
ABC_DDS_20150210_20150212_CD.csv
ABC_DDS_20150212_CD.csv

bash script - print X rows from a seleccted file from a folder

I'm trying to write a script which help to follows the logs of my application.
The logs of my application are written to "var/log/MyLogs/" with the following pattern:
runningNumber_XXX.txt , for example:
0_XXX.txt
37_xxx.txt
99_xxx.txt
101_xxx.txt
103_xxx.txt
I'm trying to write a bash script (without a success for now) which will print last 20 rows of the last log file (the last log file is the file with has the biggest prefix number).
I know I need to go over the files in the folder (for file in /var/log/MyLogs/*) and check which file name has the biggest prefix, and after it print the last 20 rows from the selected file.
please help me....
Thanks...
find /var/log/MyLogs -iname '*_xxx.txt' | sort -n | tail -1 | xargs tail -20
Get correct files
Sort numerically
Get last log file
Get last 20 rows
tail -20 $(ls -1 /var/log/MyLogs/*_*.txt | sort -n -t _ -k 1 -r | head -1)
ls -1 [0-9]*_XXX.txt | sort -rn | head -1 | xargs tail -20
Usually is the bad practice using ls in shell scripts, but if you can ensure than the logfiles doesn't contains spaces and other strange characters, you can use a simple:
tail -20 $(ls -t1 /var/log/[0-9]*_XXX.txt | head -1)
The:
ls -t sorts the files my modification time newest comes first
head the the 1st
tail print the last lines
AGAIN, this is usually a bad practice, you can use it only when you knows what you're doing.

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...!

Compare two files in Linux: ignoring first and last lines

I´d like to compare two files, but I don´t want to take into account the first 10 lines, and the last 3 lines of both files. I tried to do it with diff and tail commands, like in here, but without success.
how can I do it?
Use GNU tail and head:
To ignore the first 10 lines of a file, use tail like this:
tail -n +11 file
To ignore the last 3 lines of a file, use head like this:
head -n -4 file
You can then construct your diff command using process substitution as follows:
diff <(tail -n +11 file | head -n -4) <(tail -n +11 file2 | head -n -4)

tail and head command in Linux

While executing,
$ls -1rt /directory | head -n 3
file1.txt
file2.txt
file3.txt
$ls -1rt /directory | tail -n 3
file2.txt
file3.txt
Could anyone tell me how the tail and head works internally during file listing and why this difference in no.of files?
Thanks in advance
head lists a certain amount of lines of your file. It won’t read it integraly, just the few first lines. tail does exactly the same thing, but starts at the end of the file. The -n 3 parameter is here to stop reading after 3 lines, then prints them only.

Resources