Linux Commands

a2enmod

Enable/disable or view modules for apache server.

acpi

Shows battery and thermal information. Try with -t for thermal.

adduser/addgroup

Add a user or group to the system
For example "adduser reza" adds user reza to the system, "addgroup admin" adds group admin to the system, "adduser reza admin" adds user reza to admin group.

Add users to the end of /etc/sudoers to give them sudo privileges. That file is so flexible for example you can add root capability to a group, etc.
See usermod.
In CentOS sudo is not installed by default and you should install it first using yum install sudo.

alias

For example alias lh="ls -l -a $HOME" makes the command "lh" become equivalent to the longer command "lh -l -a $HOME". You can use it in bash profile and assign a simple alias to a long and frequently used command.

apt

apt-cache search clone Searches the packages indexes for the clone name
apt-cache show sun-java6-bin see info about that package
apt-get autoremove removes packages that were automatically installed to satisfy dependencies for some package and that are no more needed
apt-get purge x remove x with all its configuration files, etc.
apt-get remove the-package removes the-package
apt-get update Updates the package indexes
apt-get upgrade Installs the newest packages

Background Command

put & at the end of any command and it will be take to the background meaning that the command line will be freed so that you can keep typing. This is very useful and you do not need to open another command line.

bg

Shows the last stopped command using Ctrl-Z. See fg.

cal

Display a calendar

cat

Display the contents of a file
cat file_name | grep —color some_text

chkconfig

Updates and queries runlevel information for system services.
Adding to it: chkconfig —add the_script. Notice that the_script should be chkconfig friendly. See a section in linux-how-to

chgrp

Command Desc
chgrp user file Makes file belong to the group user

chmod

chmod [references][operator][modes] file1 …

Reference: u (user), g (group), o (others), a (all)
Operator: +, -, =
Mode: r, w, x, X, s, t

Example:
Command Desc
chmod ug+rw sample Adds the read and write permissions to the user and group for that file
chmod +x ss Anyone can execute the file ss
chmod og=r archive.sh others and group have read access

chown

Command Desc
chown cliff file Makes cliff the owner of file
chown -R cliff dir Makes cliff the owner of dir and everything in its directory tree

cp

Copy files and directories. To copy a dir with content use "cp -r source dest" or "cp -r * dest" copies everything in current directory to dest.

crontab

"crontab -l" show all the cron jobs for the current user.

CTRL-R

To search in the history of your commands use the CTRL-R key to perform a “reverse-i-search”.
Press CTRL-R then start typing. After you have typed what you are looking for, use the CTRL-R key combination to go through the history.
Use [Enter] to execute it. Alternatively, using the right or left arrow keys will place the command on an actual command-line so you can edit it.

date

Shows date and time

df

Display disk space information

dos2unix

Convert DOS/Windows format to ISO/Unix/Linux format. dos2unix original_file dest_file. It is good to fix specially eol/eof.

dpkg

Package manager for debian. To install a package do: sudo dpkg -i x.deb

du

Gives the disk usage.
du -sh /dir gives the directory size. h stands for human readable and s is for summary.

env

Environment variables

export

  • export shows some env stuff
  • export CASROOT=/usr/local/CAS3.0 Defines the environment variable CASROOT
  • export PATH=$PATH:$HOME/bin I am setting the PATH environment variable to the previous value of itself, followed by a colon, and a directory-name formed from the HOME variable and "/bin"

echo

echo $CASROOT Prints out the value of CASROOT, or /usr/local/CAS3.0

egrep

Similar to grep but understands full regular expressions: cat file | egrep "NAME|VERSION"

fg

Goes to the last stopped command using Ctrl-Z. See bg.

find

Searches for files by name, etc.

Command Desc
find search_path -name filename Normal usage
find . -name aaa.txt Finds all the files named 'aaa.txt' in the current directory or any subdirectory tree
find . -name *.sql accepts wildcards
find / -name vimrc Find all the files named 'vimrc' anywhere on the system
find /usr/local/games -name "*xpilot*" Find all files whose names contain the string 'xpilot' which exist within the '/usr/local/games' directory tree
find / -name 'mypage.htm' search for any file named mypage.htm on the root and all subdirectories from the root
find /backups/ -type f -mtime +5 -exec rm {} \; Finds are the files in that directory older than 5 days and deletes them!

If you get "Permission denied", do it with sudo.
You can combine find and grep. As an example: find . -name build.xml | grep java

free

Displays the amount of free and used memory in the system. Use free -m to display in megabytes.

ftp

Command Desc
ftp goes into ftp shell
open open www.xxx.com to open the remote host using port 22
cd change directory after login
get get a file and copy into your local folder
put upload a file into the host
binary or ascii To transfer a file in binary or ascii mode. Some ftp clients go back to ascii mode when you enter an ls or dir command. To be safe, always use the binary command before getting a binary file.

gksudo

Is a front end for sudo which sucks.

grep

Is a very powerful search tool! Can search the content of files or output of another command.

Options Desc Example
grep test *.* search for the test in all file in the same directory similar to grep test . or grep "test" .
-r Resursive grep -r "reza" . OR in SunOS grep searchText *.*
-d recurse same as -r
-n line number
-i case in-sensitive
—color add color

groupmod

modify groups.

groups

Returns the user's groups. See whoami and id.

gzip

gzip -dc file | less

history

A list of previously ran commands. You can do !! instead too.

ifconfig

/sbin/ifconfig -a

jobs

Shows stopped commands using Ctrl+Z.

kill

kill <PID> kills a process . if it does not work then use kill -9 <PID>. See pkill

last

shows information about the last logged in users, etc

ls

Option Desc
-l List showing date, owner,…
-a Shows hiddens as well
-lt List sort by time

When you do ls -l you will get:

-rw-r--r-- 1 jboss jboss  2481 Feb 25 12:43 run.conf
file type read/write/exe permissions for owner/group/others # of links owner group size date name
- rw - r - - r - - 1 jboss jboss 2481 Feb 25 12:43 run.conf

There are three types of access:

  1. read (r)
  2. write (w)
  3. execute (x)

There are three types of files:

  1. ordinary file (-)
  2. directory (d)
  3. symbolic link (l)

lsmod

Show status of modules loaded into kernel. It resides in /sbin

lsof

List of open files and processes that opened them. It includes disk files, ports, etc.
For the ports do:

sudo /usr/local/bin/lsof -i :80

lspci

Lists all pci hardware devices connected

ln

create a hard or soft link.

By default, every file has a single hard link that gives the file its name. When we create a hard link, we create an additional directory entry for a file. A hard link is indistinguishable from the file itself. It is helpful to imagine that files are made up of two parts: the data part containing the file's contents and the name part which holds the file's name. When we create hard links, we are actually creating additional name parts that all refer to the same data part.

(hard link) ln <origin> <new location/filename>
(soft link) ln -s <destination> <nickname>

ln -s /x/file.ext : makes a symbolic link in the current dir to file /x/file.ext

mount

mount by force when you get mount errors: sudo mount -t ntfs-3g /dev/sdb1 /media/Network -o force
The better way is to connect the device, restart the system and everything should be good.

mv

Rename or move files.

netstat

Networking information
netstat -an show which ports are listening, etc.

nmap

This utility is for network exploration or security auditing.

nmap -p 80 localhost tells the status of port 80
sudo nmap -sT -O myip all open ports on myip

nohup

nohup command &

No Hangups. When a command is invoked using nohup then SIGHUP signals are ignored. User can even sign out and the process will continue to run. This will generate a nohup.out log file.
If you do not want the nohup.out log file: nohup command 2>/dev/null 1>/dev/null &

nslookup

nslookup -q=mx yourdomain.com check mx records
nslookup -debug verbose

pargs

Examines a process and prints related information such as arguments and environment variables, etc.
For example pargs<PID> is useful with ptree and ps.

pkill

kill a process by name or other features. pkill python.

pstree

Shows processes in a tree format. It is usually used as pstree <PID>.

ps

view processes
Command Desc
ps -ef | grep 'jboss4/bin' Filters and shows (e)very process in a (f)ull list.
sudo ps -Af SunOS

reboot

"sudo reboot" reboots the system.

read

read -p "press enter to continue"

this stops the batch file and waits for the enter key.

rm

rm removes files. To remove directories use -fr option.
removing multiple files or directories in multiple locations: find . -name "FILE-TO-FIND"-exec rm -rf {} \;

rsync

Usual command for me was rsync -avzP. At the time of this writing rsync v3 (2013) has memory issues with large number of files. My QNAP NAS restarts during the operation with out of memory error.

scp

Remote copy: scp local_file usrname@remote_host:remote_file. If using Windows as the client, it is easier to use WinSCP. scp local_file usrname@remote_host: will copy the local_file into the home folder or user "usrname". Notice the colon. If you want to copy to the remote machine without being asked for password read the section in the HowTo page.

sed

Can edit text on a stream.

id=$(eval $xxx | grep -Po '"id": ".*"' | sed 's/"id": "' | sed 's/"')

s flag is for substitution.

if we have daynight string in a file called test then command cat test| sed 's/n/y/' will replace n with y and returns dayyight.

shutdown

To shutdown or restart the system.

shutdown -P now shutdown now
shutdown -P +30 shutdown in 30 min
shutdown -P 22:10 shutdown at a specified time
sudo reboot to restart the system

source

Same as exec command but executes in the same shell without the need to create a new process.

source .bash_profile

sudo

Run commands as another user.
sudo -u jboss x.sh runs the file as a jboss user. When used without -u then it is root.

tar

Command Desc
tar xvf archive.tar Extract from the archive file
tar cvf archive_name.tar dirname/ Create a new tar archive
tar cvfz archive.tar.gz SOURCE Create a gzip compressed tar archive containing everything in the directory 'SOURCE'. You can create archives with gzip,… as well.

tail

tail -f myfile.txt -n 100 : list the last 100 lines in the file myfile.txt. and stays there to follow

top

Shows a real time view of processes, memory usage, load, CPU state, etc.

touch

Change the time of the file. It is useful for times when a changed access time will trigger another operation…

uname

Useful information about kernel, version, distribution, machine, ….

uptime

how long the system has been up for

usermod

modify a user account. Can be used to rename users. If you rename a user don't forget to change these: /etc/passwd, /etc/group, /etc/shadow and home directory.

usermod -a -G root reza : adds user reza to roots users.

users

Prints the users currently logged in to the system.

view

view filename = vi or vim

vi commands

Command Desc
i Enter text entry mode (Esc to exit)
ZZ Write (if there were changes), then quit
:wq Write, then quit
:q Quit (will only work if file has not been changed)
:q! Quit without saving changes to file
x Delete a character (in command mode)
dd Delete a line (in command mode)
Esc leave insert (text entry) mode and return to command mode
yy Copy (yank) the current line
R Overwrite text, press <Esc> to end
6yy Copy (yank) six lines, beginning with the current line
yw Copy the current word
p Put the text after the cursor position
P Put the text before the cursor position
/text Search forward (down) for text. Can be used when we have / in the search text! Press n or shift+n for next and previous.
shift-n find next
?text Search backward (up) for text
:set nu Display line numbers
:set nonu Hide line numbers
gg Goto start of the file
G Goto end of the file
:n line n; use :0 to move the beginning of the file
Ctrl-b page up
Ctrl-f page down

visudo

grants sudo access to usres
reza ALL=(ALL) ALL gives all the sudo permissions to reza
reza ALL=(ALL)NOPASSWD:/sbin/shutdown asks no password from reza when running shutdown. This should be placed after above line. order matters

which

Shows the physical location of a command for example "which ls" tell where the "ls" is located in the disk.
yum install wget
yum update

yum

yum - Yellowdog Updater Modified, is the equivalent of apt-get for RH/CentOS.

Key combinations

  • Ctrl-C: Usually sends the kill signal, which terminates a process.
  • Ctrl-Z: Sends the stop signal, which stops process execution immediately, without terminating. Use “fg” or “bg” after that.
  • Ctrl-L: Clears the screen and works like clear command.

wc

counts the number of lines/bytes/words/characters in files. "find | wc -l" finds the number of lines in all the files of the current directory.

wget

Get a file from the network. very useful!

who

shows who is logged in.

xev

Shows a window (in X server) and sends all X server events to it. It is good for debugging keys, mouse, etc.

xmodmap

Change/view key maps

Other References

A reference by Perpetual PC's

Reference

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License