Saturday, July 23, 2016

Linux commands for the newbie

Many developers become intimidated using the Linux command line for the first time. Below are some introductory commands to help you get started.

Navigation
pwd                                      Get current directory
ls -l                                    File listing

Directories
du -k                                    Get size of all subdirectories in KB
du -m                                    Get size of all subdirectories in MB

Copying Files
cp -r en en.backup                     Copy directory recursively to new one

Symbolic Links
ln -s /u01 newlink                     Create a symbolic link

Find
du -S | sort -n | tail -20             Finding the largest directories
ls -lR | sort +4n | tail -20             Finding the largest files
find . -name filename.txt                Find a file in hard disk
which java                               Locate command, display pathname/alias
find / -perm -2 ! -type l -ls            Find all world writable files
find / -nouser -o -nogroup -print        Find files with no owner and no group
find . -type f | xargs grep -n "text"    Find text in string

Find Files > 20 MB
find . -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
find . -size +20000k -exec du -h {} \;
find . -size +20000k -exec du -h {} \; | sort -n | more

Files
cat filename.txt                       List contents of file
more filename.txt                      List contents of file, with page pause
tail -f filename.log                   List contents of file, with scrolling
head -10 filename.log                  List first 10 lines of a file
tail -10 filename.log                  List last 10 lines of a file
grep -i text test.log                  Search for "text" in file, case insensitive
grep text test.log                     Search for "text" in file, case sensitive

Filesystem
df -k                                  File system in Kb
df -h                                  File system in MB
df -m                                  File system in MB

Processes
ps -ef                                 View all processes on system
kill -9 12345                          Force kill process id 12345
killall a.out                          Kill all processes
free -m                                Check the amount of free memory
ps -efaux                              View all processes hierarchically
top                                    Running processes; cpu & memory usage
pstree -p                              Shows running processes in tree format
ps -aux | sort +5n | tail -20          Find processes that are largest
  
Zipping
gzip download.tar                      Compress file to .gz file
gunzip download.tar.gz                 Uncompress
tar -xvf download.tar                  Extract
tar -cvf newfile.tar *.bmp             TAR the files
gtar -xzvf source.tar.gz onefile.java  Extract only one file
gtar -xzvf webmail*.tar.gz             Extract compressed file
gtar -czvf source.tar.gz directory     GTAR + compress

Java
jar tvf test.jar                       View contents of JAR file
jar xvf test.jar                         Extract contents of JAR file
  
Network
netstat -nr                            Show routing table
netstat -na | grep 7001 | grep LISTEN  See if exists process on port 7001
traceroute 200.200.200.23              Traces route & hops to target ip
tcpdump                                Dump traffic on a network

RPM Packages
rpm -e --test anonftp-3.0-9            Not real uninstall
rpm -i anonftp-3.0-9.i386.rpm          Install RPM package
rpm -q kernel                          Short kernel version
rpm -qa                                Query the RPM database
rpm -qa | grep perl                    Query the RPM for anything 'perl'
rpm -qai                               Query + information
rpm -qia                               Query all detail
rpm -e anonftp-3.0-9                   Uninstall RPM package

Host
hostname                               Display hostname of server
uptime                                 Uptime of server
uname -a                               Show kernel version
uname -r                               Short kernel version
dmesg                                  Find out information
rpm -q kernel                          Short kernel version
rpm -qi kernel                         Detailed kernel version
nmap localhost                         Check open ports

Shell
alias bdf='df -h'                      Create alias
alias bdf                              Query alias
set -o vi                              Set command thing to vi
env                                    Show environment
export ORACLE_SID=demo                 Set an environment variable (Bash shell)
setenv ORACLE_SID demo                 Set an environment variable (C shell)
echo $CLASSPATH                        Query classpath environment
history                                Show history
umask 027                              Default create permissions of 750
echo $SHELL                            See what shell you are running
export PS1='\u:$PWD> '                 Set prompt

Shutdown
shutdown -h 10                         Shutdown and halt in 10 seconds
shutdown -r 10                         Shutdown and reboot in 10 seconds
poweroff                               Power off
halt                                   Halt

User
id                                     Information on current user
id webuser                             Information on user 'webuser'
groups                                 Groups of current user
groups webuser                         Groups of user 'webuser'
gpasswd                                Administer the /etc/group file
last -20                               Show last 20 logged in users
useradd, usermod, userdel              User admin commands
groupadd, groupmod, groupdel           Group admin commands

Performance
top                                      Realtime
iostat 2                                 CPU and IO stats every 2 seconds
vmstat 5                                 System statistics every 5 seconds

Miscellaneous
ll ; ll                                Run multiple commands using ;
ulimit                                 Set or get limits on system resources
ulimit -Sa                             Check if shell has limits on file size
ulimit -Ha                             Check if shell has limits on file size
chkconfig --list                       Query runlevels



No comments: