Get latest updated file from your Current Working Directory
Hi, I am student of BSC.I.T. and wanted to know more about UNIX. So, I am making R&D in unix shell script. So, can anyone explain me how to get the name of the file which is latest updated in that directory? Please give me the command and if possible explain it.
Re: Get latest updated file from your Current Working Directory
Hi, I am also new to this UNIX programming and not interested in this. But, if you have interest in this then just try to use of internet and download books regarding it. As it may provide you more knowledge and commands regarding it. Or make search for the commands regarding what you want. You can buy book of Orelly to get more advanced knowledge. Just make use of it. So, that you can be able to know more.
Re: Get latest updated file from your Current Working Directory
Hi, you want to get latest updated file from your current working directory, So if your CWD contains only files and directories. Then you can use following command:
Code:
cd <your existing folder>
filename=$(ls -1t|while read obj
do
echo $obj
break
done)
echo $filename
Re: Get latest updated file from your Current Working Directory
Hi, I am not having that much information in Unix. But as you want to know the latest updated file from your current working directory you can use just following command which will solve your problem I think. Just try this.
Code:
latestfile=`ls -1tr | tail -n 1`
This would give the latest updated file from the directory in which you are currently and it will store that filename in the variable.
Re: Get latest updated file from your Current Working Directory
hey, buddy the following line gives u latest updated file (it can be directory also)
LATEST_FILE=`ls -1tr | tail -n 1`
OR
LATEST_FILE=`ls -ltr | tail -1 | tr -s '[:space:]' '[\:*]' | cut -d ":" -f 10`
here the LATEST_FILE is a variable which contains latest updated file