Useful commands

= Performance =
top                                             ; shows running processes; 
                                                'c' = see process details; '1' = show processor cores; 'k' = kill a process
                                                'A' = show many windows; 'a,w' = navigate windows; 'z' = color; 'x' = highlight sort col
                                                shift-w = save it too your .toprc
= Finding Stuff =
find / -name "file_name" -print                 ; find file name from root directory path (/)
find . -user root                               ; find files owned by root user, in current directory (.)
locate -i fstab                                 ; find file name in database that keeps track of filenames
find . -type f -user zenoss -name "*.py" -exec grep -il "blah" {} \;
                                                ; find text "blah" in .py files belonging to zenoss user
locate Data.fs | xargs ls -l                    ; xargs to pipe results of one command to another
find ./ -name '*py' | xargs grep signal         ; Find string "signal" inside text of .py files; xargs looks inside the files instead of at the file name
grep -ir "timeout" .                            ; find the string "timeout" in any file below the current dir
= Various =
screen                                          ; lets you share screens with other people
nano                                            ; text editor that lets you use modern keyboard keys
psql <database name>                            ; connect to postgres database in postgres command prompt
/sbin/service postgresql restart                ; restart the postgres server
/sbin/service httpd restart                     ; restart the http/web server (ussually apache)
ps axo pid,tty,stat,bsdstart,bsdtime,command    ; list processes with time started and cpu time taken
ps -aux                                         ; easier process list, alsp try without the '-'
ps -ef                                          ; short columned process list (less fields returned)
smbd &                                          ; postfix with '&' to have it run in the background
wget <http url>                                 ; download a file from the web
sar                                             ; system activity reporter
du -k ~ | more                                  ; show disk usage (useful for over quota) for home directory
du -h --max-depth=1 . | sort -rn                ; show totalled directory sizes for current dir
uname -m                                        ; how to tell if linux is 32 or 64 bit - i386 or i686 then it is 32 bit, x86_64 is 64bit
chkconfig                                       ; services on / off
crontab -l                                      ; list the existing cron instances
crontab -e                                      ; add crontab entries for current user
dmesg | more                                    ; print out bootup messages / kernel ring buffer
whoami                                          ; tells you who you are currently logged in as
su                                              ; set user, often used to escalate to root to run something special
cp -Rf /home/cvs/* /usbdrive/cvs/               ; backup, simple; recursive, force
cp -Rfuv --reply=yes /home/cvs/* /usbdrive/cvs/ ; backup, update; copy new files only, verbose, dont ask overwrite
dmesg | grep -i failed                          ; search bootup messages for failures
ifconfig -a                                     ; find IP address and other network details
hostname                                        ; show current machines hostname, also can set new one and do other stuff, see man page
useradd, usermod                                ; add or modify user access rights, group rights (primary, secondary)
alias ll='ls -la'                               ; make an alias for the long list command, now just type 'll'!
unzip -ql                              ; see the zip files contents
tar xvfz something.tgz                          ; untar a .tar.gz file
tar -cjf wiki.121308.tar.bz2 ../wiki            ; quick backup command to tar file
nohup <any_command> &                           ; "NO HangUP", runs a command continually in background, even if user who ran it logs out
diff -qr /transit2/ /documents/transit2/        ; compare directories, do not show specific file differences, recursive
chkconfig --list                                ; startup service information listing; shows which services are set to run at boot
service smtpd status/stop/start                 ; Email service stop/start; /var/log/maillog for some debug info
for i in *.txt; do mv "$i" `basename $i .txt`.doc; done    ; Rename all txt files to doc
/usr/bin/ssh -i ~/.ssh/id_dsa server_name "ls"  ; run command on another server and get results; id_dsa is ref to PKI login info... no passwd needed
ps -A -o pid,state,command | grep D             ; find process that's blocking due to IO wait
                                                ; 'D' is for Delayed processes, IO wait is most common cause
= File System Manipulation =
fuser -m /dev/md0                               ; shows processes being used by device
lsof /media/documents/                          ; show list of files open
lsof / | grep media                             ; in case above doesnt work, lsof seems to work better on root and then grep it out for what you need
lsof +D /var/log/                               ; list of files open under the given directory (recursive) (use +d for non-recurse)
= Network Information =
nslookup                          ; reverse DNS lookup; gives the hostname associated with the IP
host -a                          ; DNS lookup; gives IP associated with the hostname (verbose)
dig ns zenoss.loc                               ; gives nameserver of domain
netstat -anp | grep 8082                        ; will tell you what process is listening at 8082, if any
netstat -an | grep LIST                         ; show listening ports (LIST = LISTEN)
tcpdump ip proto ICMP                           ; shows you what ping requests are being send and received, realtime
tcpdump port 80 or port 443                     ; shows traffic in and out on http and https
echo “newhostname” > /proc/sys/kernel/hostname  ; set new hostname on CentOS / RedHat
netstat -rn                                     ; routing table
= Package Management =
rpm -qa                                         ; Packages installed on system
rpm -qa | grep httpd*                           ; See if httpd is installed
yum list installed httpd                        ; See if httpd is installed
yum provides /etc/fstab                         ; name of the package which contains a particular file
yum list                                        ; lists information about available packages
yum install gnote                               ; install gnote and all its dependancies
patch < /patch/to/apply.diff
patch -R --dry-run --verbose < patch.diff       ; undo / backout a patch
yast                                            ; opensolaris / SLES package manager

Also, checkout ‘nachos’ useful command KnowledgeBase:

If link is lost, content is duplicated here: NachoKB

Command usage example

Below is a modified version of real shell commands from here

ps ax | grep nps           ; returns all currently running processes with the string 'nps' in it. See if one with a reference to 'umbc' in its name is running
nano /etc/inittab          ; opens a text editor for the inittab file. The [[inittab|]] file describes which processes are started at bootup and during normal operation . Make sure that the line references the posting daemons are not commented out with a #
cd /var/para/blah/         ; changes your current directory
ls                         ; lists the files and directories in the current directory
php blah_umbc_import.php   ; executes the file nps_umbc_import.php . it goes into a endless loop; end it by hitting ctrl+C
pwd                        ; print working directory. Shows you which dir you are currently in
/sbin/telinit Q            ; reestablish all inittab settings and processes
ps ax                      ; look for the umbc referencing process in the resultant list
\rm -r myDirectory         ; remove the directory 'myDirectory' recursively and with no prompts
while [[ 1 ]]; do echo $RANDOM; sleep .01; done
                           ; Peg (use up) a single CPU core; modify the sleep to increase or decrease amount of CPU used
technology/unix/useful_commands.txt · Last modified: 03.18.2013 05:53 by
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki