Skip to main content

Delete 10 newest files in Linux

Delete 10 newest files in Linux

The code you'd want to include in your script is
 rm -f $(ls -1t /path/to/your/logs/ | tail -n +11)
The -1 (numeric one) option prints each file on a single line, to be safe. The -f option to rm tells it to ignore non-existent files for when ls returns nothing.
Or use the below command
ls -d -1tr /path/to/folder/* | head -n -10 | xargs -d '\n' rm -f

Delete latest 3 files:
rm -f $(ls -1t ./ | head -3)   

Find a folder
Just find directories and skip file names pass the -type d option as follows:
find  / -type d -name "apt" -ls

locate httpdocs


Gives the time stamp

date +%Y%m%d -s "20081128"
date +%T -s "11:18:00"

Stat file.txt

newest=$(find subdir -newer timestamp -printf "%A%p\n" |
                sort -n |
                tail -1 |
                cut -d: -f2- )
     touch -r "${newest:-timestamp}" timestamp
The command above works by generating a list of the timestamps and names of all the files which are newer than the timestamp. The sort, tail and cut commands simply pull out the name of the file with the largest timestamp value (that is, the latest file). The touch command is then used to update the timestamp,
The "${newest:-timestamp}" expression simply expands to the value of $newest if that variable is set, but to timestamp otherwise. This ensures that an argument is always given to the ‘-r’ option of the touch command.

Comments

Popular posts from this blog

Basic shell scripting part 1

What is $*? Its mainly used for showing up all params. This show all parameter values passed in shell script What does $# stand for? # will return the number of parameters that are passed as the command-line arguments. What does $? Return? $? will return exit status of command .0 if command gets successfully executed ,non-zero if command failed. What are Different types of shells? sh : the oldest shell  csh : C shell  ksh : Korn Shell  bash : bourne again shell  How do you read arguments in a shell program – $1, $2? Shell script accepts parameters in following format…  $1 would be the first command line argument, $2 the second, and so on  $0 is the name of the script or function If your script has more than 9 params then accept in following way…  ${12} : 12th param  ${18} : 18th param What are the different kinds of loops available in shell script ? for, if, while, case What is the difference...

Linux and Maven Commands

Maven mvn -Dmaven.test.skip=true -e clean install mvn clean install mvn clean install -Dmaven.test.skip=true Linux: telnet mq.dev.sum.org  9012 telnet <machine_IP> <port> Sudo su Su - sudo -u dev2adm  -i ps -ef|grep jboss Find a folder Just find directories and skip file names pass the  -type d  option as follows: find   / - type d -name "apt" - ls locate httpdocs Delete latest 3 files: rm -f $(ls -1t ./ | head -3)   working fine ls -d -1tr /path/to/folder/* | head -n -10 | xargs -d '\n' rm -f rm -f $(ls -1t ./ | tail -n +4) netstat -ln | grep '80 ' | grep 'LISTEN' netstat -atnp|grep LISTEN  netstat -ln | grep '99 ' | grep 'LISTEN'  netstat -ln | grep '16'  scp -pr Services-0.0.2-SNAPSHOT.war sbiswas@172.1.8.141:/home/sbiswas tail -f /tmp/Liferay-JBoss.out |grep 'PAGENAME\|tabName' grep 'PAGENAME\|tabName'  for multiple se...

Hiding a file or directory under Linux

Hiding a file or directory under Linux To hide a file or directory in Linux, just give it a name preceded by a full stop (.). For example: . bash_history . bash_profile . ssh    Hide UNIX file structure   1. Open a terminal and execut "ls -1 / | sudo tee /.hidden" 2. Do "sudo chmod a+r .hidden" 3. If you want all standard files hidden, skip to 5. Otherwise. Open the file with "sudo gedit /.hidden". 4. Remove every directory you don't want to hide from the file. Here's how mine looks like, as an example: bin boot opt usr etc sys tmp srv proc root sbin lib lost+found mnt initrd dev debootstrap var cdrom initrd.img initrd.img.old vmlinuz vmlinuz.old home media 5. Now, let's create some entries that are easier for new users: select "home" and create a link to link to it (for example, by dragging it and holding <ctrl>+<shift>. Your mouse cursor shou...