cancel
Showing results for 
Search instead for 
Did you mean: 
kubeadm
Flight Engineer Flight Engineer
Flight Engineer
  • 16.5K Views

What's your favorite tool/cmd in Linux?

The command "find" is one of my favorites -

Search for files in /search/path/  which are older than 5 days.

(if you want to search for directories, then use:   -type d )

find /search/path/ -type f -mtime +5 -print

find /search/path/ -type d -mtime +5 -print

If you want to do something with those files after you've found them, use:  -exec <somecmd> '{}' \; 

Example, delete files older than 50 days in the /search/path directory

find /search/path/ -type f -mtime +50 -exec rm '{}' \;

 

If only there is a 'find' equivalent in the real world ... now, where did I put my keys? 

Tags (2)
49 Replies
BramMertens
Flight Engineer
Flight Engineer
  • 4,599 Views

Another great tool to use that is often overlooked is xargs.

Combined with find, ls, echo, grep etc. you can achieve powerfull combinations.

The more I learn to use vim the more I'm impressed by what it can do.

On the fly recorded macro's have helped me several times to generate or update large number of complex (SQL) statements etc.

It's built in powerful search and replace has prevented countless hours of tedious work.

And a third huge time saver that surprisingly many people are unfamiliar with is ssh-agent. Being able to easily and securly log on to many machines is a great time saver as well.

RHCE 100-015-239
0 Kudos
  • 4,580 Views

lsof. So much lsof. So useful for tracing back a port that's in use to a running process, and then for diagnosing beyond that.

0 Kudos
Lars
Flight Engineer Flight Engineer
Flight Engineer
  • 4,591 Views

One of the most important cmd especially when you're working remote is tmux!

0 Kudos
AngelReynoso
Mission Specialist
Mission Specialist
  • 4,586 Views

I like all the Linux tools .

Jeff_Schaller
Flight Engineer
Flight Engineer
  • 4,572 Views

I'd follow on to the


@kubeadm wrote:

If you want to do something with those files after you've found them, use:  -exec <somecmd> '{}' \; 

Example, delete files older than 50 days in the /search/path directory

find /search/path/ -type f -mtime +50 -exec rm '{}' \;

 

example by suggesting two enhancements:

1. Use -delete for the specific case of removing files or directories; it saves having to call out to another program just to delete something. It was added in 2004 , so hopefully (!) it's on the majority of systems you're working on. It also "does the right thing" by turning on -depth mode, so you don't end up trying to remove a directory before it's empty.
2. Use "-exec command {} +" for the more general case of executing a command against multiple files. Introduced in 2005 , this is useful for any command (such as rm) that accepts multiple files as parameters to work on. It's similar to xargs, but avoids the problems with passing filenames through a pipe.

Jeff_Schaller
Flight Engineer
Flight Engineer
  • 4,575 Views

I'd have a hard time choosing a favorite; the most frequent are boring (ls, cd) or opinionated (vi/vim), and there are powerful candidates like ps, grep, awk, etc; but I'll propose "sudo" as my favorite. It enables fine-grained elevated permissions and I enjoy using it as a sysadmin just to keep my normal prompt un-elevated (in case of accidents!) and to log what I (and other) sysadmins type, again, usually to investigate an accident.  But mainly to limit the potential damage when a non-sysadmin needs to do a few specific things with elevated privileges.

0 Kudos
Vinícius_Boas
Flight Engineer Flight Engineer
Flight Engineer
  • 4,568 Views

awk/gawk ... Everything is possible with awk/gawk + shell script! :)

 

In Red Hat I trust, everything else, I test!
  • 4,563 Views

I have a couple of favorites that have become standard aliases on any system I use regularly:

  alias top10=' ls -la | sort -rn -k 5 | head '
  alias topdu=' du -sk .[0-9A-Za-z]* [0-9A-Za-z]* 2>/dev/null | sort -rn | head '

"top10" lists the 10 biggest files in the current directory, and "topdu" shows the 10 subdirectories under the current directory that are taking up the most space.  In either case you can add "-#" to change the number of lines output.  For example "top10 -20" shows the 20 biggest files.

 

0 Kudos
Jeff_Schaller
Flight Engineer
Flight Engineer
  • 4,557 Views

@Gordon-Keegan, if you work exclusively on RedHat systems -- or, I should say, GNU/Linux systems with GNU coreutils' ls, then you may be interested in:

alias top10='ls -laS | head '

as that will instruct "ls" to do the sorting by size, versus having to rely on field 5 being the size. Consider a system that's integrated with LDAP or Active Directory and a user's primary group name contains spaces.

Reference: ls invocation -- 10.1.3 Sorting the output

  • 4,551 Views

TIL ! Thanks for the tip, it's apparently been too long since I reviewed the cmdline args :)   Hmmm...   That means I should probably review other commands.

0 Kudos
Join the discussion
You must log in to join this conversation.