Linux command most recent non soft link file

179 Views Asked by At

Linux command: I am using following command which returns the latest file name in the directory.

ls -Art | tail -n 1

When i run this command it returns me latest file changed which is actually soft link, i wants to ignore soft link in my result, and wants to get file names other then soft link how can i do that any quick help appreciated.

May be can i specify regex matched latest file file name is

rum-12.53.2.war
2

There are 2 best solutions below

0
On BEST ANSWER

-- Latest file in directory without softlink

ls -ArtL | tail -n 1

-- Latest file without extension

ls -ArtL | sed 's/\(.*\)\..*/\1/' | tail -n 1
1
On

The -L option for ls does dereference the link, i.e. you'll see the information of the reference instead of the link. Is this what you want? Or would you like to completely ignore links?

If you want to ignore links completely you can use this solution, although I am sure there exists an easier one:

a=$( ls -Artl | grep -v "^l" | tail -1 )

aa=()
for i in $(echo $a | tr " " "\n")
do
    aa+=($i)
done

aa_length=${#aa[@]}

echo ${aa[aa_length-1]}

First you store the output of your ls in a variable called a. By grepping for "^l" you chose only symbolic links and with the -v option you invert this selection. So you basically have what you want, only downside is that you need to use the -l option for ls, as otherwise there's no grepping for "^l". So in the second part you split the variable a by " " and fill an array called aa (sorry for the bad naming). Then you need only the last item in aa, which should be the filename.