Skip to main content

Posts

Showing posts from October, 2014

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

SED COMMAND TO DELETE LINES IN FILE

Sed Command to Delete Lines : Sed command can be used to delete or remove specific lines which matches a given pattern or in a particular position in a file. Here we will see how to delete lines using sed command with various examples. The following file contains a sample data which is used as input file in all the examples: > cat file linux unix fedora debian ubuntu SED COMMAND TO DELETE LINES - BASED ON POSITION IN FILE In the following examples, the sed command removes the lines in file that are in a particular position in a file. 1.  Delete first line or header line The d option in sed command is used to delete a line. The syntax for deleting a line is: > sed 'Nd' file Here N indicates Nth line in a file. In the following example, the sed command removes the first line in a file. > sed '1d' file unix fedora debian ubuntu 2.  Delete last line or footer line or trailer line The following sed comm...