Linux Commands
Useful Linux Commands
You can use man [command]
to get information about a command, but that is more programmer oriented. You are generally better off going to a search engine and searching for [command] examples
.
One of the great powers of Linux is being able to pipe |
or redirect outputs from one command to another to do things.
You can also use these commands to create bash scripts.
Contents |
Disk Commands
df Disk Free
Returns a quick overview of what disks are mounted and how full they are in human readable formatting.
df -h
mount Mount a file system
Mount a file system or device. You can also type mount by itself to show current mounts on the system. Before making the mount, make sure you have a directory on the Linux box to mount the filesystem to. Usually mounts go under the /mnt directory.
Create a mount point
mkdir /mnt/whatever
Once you have the directory set up, you can now mount the filesystem or share.
Linux mounts use nfs usually:
mount -v -t nfs 10.1.1.20:/exportdir /mnt/whatever
Netware mounts use npc usually:
ncpmount -S [server] -A [IP address] -U [fully distinguished name] -V /vol1 /mnt -o tcp
Example:
ncpmount -S school -A 192.168.100.1 -u admin.wwws -V vol1 /mnt/nwserver -o tcp
Windows mounts can use cifs or samba:
mount -t cifs //ntserver/download -o username=admin,password=password /mnt/whatever
mount -t smbfs -o username=admin,password=password //ntserver/download /mnt/ntserver
Note: Reload comes with some nice scripts that Tay created for setting up cifs and ncp mounts. They are found under /opt/beginfinite/reload/setup. You just enter the setup information into the corresponding conf file and run the script.
cifsmnt.conf:
- Author: Tay Kratzer taykratzer@taykratzer.com ##
- All values must be kept within double quotes. ##
CIFS_USERNAME="administrator" CIFS_PASSWORD="windoze" CIFS_SHARE="share1" CIFS_SERVERIP="137.65.55.210" CIFS_RETRY_COUNT="10" CIFS_RETRY_SECONDS="30" LINUX_MOUNT_POINT="/mnt/winserver"
ncpmnt.conf:
- Author: Tay Kratzer taykratzer@taykratzer.com ##
- All values must be kept within double quotes. ##
NCP_LOGIN_NAME="admin.acme" NCP_LOGIN_PASSWORD="novell" NCP_VOL="sys" NCPVOL_SERVERIP="137.65.55.210" NCPVOL_SERVERNAME="wwwfs1" NCPVOL_MAP_RETRY_COUNT="10" NCPVOL_MAP_RETRY_SECONDS="30" LINUX_MOUNT_POINT="/mnt/nwserver"
Once you've set up the conf file, run the script from the command line.
umount Unmount filesystem
umount – Unmount a device or mounted filesystem.
testserver:/ # umount /mnt/whatever
Directory Commands
cd Change Directory
Change directory. Tab complete can help traverse the tree.
cd [destination directory]
Change to root directory
cd /
go up one level
cd .. ..
go up two levels
cd ../.. ...
du Directory details
Returns an estimate of file space usage, in human readable format (k,M,G). Warning: May take a long time.
du -h
Returns a total size of the directories under where this command was run and sorts by size.
du -hsx * | sort -rh | head -10
md Create a directory
Creates a directory
md [directory name] mkdir [directory name]
cp -R Copy a directory
Copy a directory recursively "R"
cp -R "source" "destination"
rm -r Remove a directory
Removes a directory and its contents. Warning: irreversible, fast and dangerous if done in the wrong place with wrong switches. [1]
rm -r "directory"
l Directory List
Return a list of files in a directory. This always works (shows file size in bytes)
ls -s
This is a SLES alias of the above
l
Display file size in human-readable values "-h" (K,M,G)
l -h
Sorted by size "S", ascending "r", human-readable "h"
l -Srh
Sorted by time "t", ascending "r", human-readable "h"
l -trh
Show hidden files "a", long list format "l", human-readable "h"
l -alh
Sorted by reverse chonological order l -qaltr
List all open files
lsof
chmod Change Permissions
Permissions are a big deal in Linux [2] not so much in Windows.
Add read and write permissions to User and Group but only read to Other recursively "R" in the destination directory
chmod -R 664 /[destination]
Add Execute, read, and write permissions to User and Group but only read to Other recursively "R" in the destination directory
chmod -R 774 /[destination]
Add all the permissions
chmod -R 777 /[destination]
chown Change Ownership
Ownership of a directory can make a big difference in how Retain works
chown user:group directory/
Change the ownership recursively "R" to user tomcat and group tomcat of the logs directory in the current directory
chown -R tomcat:tomcat logs/
File Commands
cat Concatenate
Show the contents of a file
cat "filename"
Pipe through more to display a screen full of text at a time (space for next page, enter for next line, ctrl-c to break out)
cat "filename" | more
Pipe through less to display file in less.
cat "filename" | less
find Find a file
Find a file from the root of the file system (aka looks everywhere for it) Warning: may take a long time.
find / -name "fileName"
Find all files with the string "log" as part of the filename.
find . | grep log
Find and recursively unzip all items in a directory structure called zippedDir to the current directory.
find /zippedDir/-name "*.zip" -exec unzip {} \;
Find files in current directory older than 7 days and delete them.
find . -mtime +7 -exec rm {} \;
Find any file off the root and beyond that is larger than 20M:
find / -type f -size +20M -exec ls -lh {} \; | awk '{ print $NF ": " $5 }'
Find the number "wc" lines "l" of files "-type f" in a directory structure:
find [directory]/ -type f | wc -l
Report all known instances of a command. A quick way to find out if something is installed.
whereis [commandname]
cp Copy file
cp origin /destination/file
To copy particular files recursively
cp -r *.txt /destination
To copy to the current directory
cp /software/origin.file .
To secure copy a file to a remote location
scp [source] [destination IP address]:[username]/[destination]
Sync a file or directory between locations, recurse "r" through subdirectories, retain "a" permissions, ownership and timestamps, verbose "v" logging to screen, show progress "P"
rsync -ravP [source] [destination]
mv Move file
Moves file to the destination directory in verbose "v" mode.
mv -v "file" "destination"
rm Remove or delete a file
Removes a particular file
rm "file"
chmod Change Permissions
Permissions are a big deal in Linux [3] not so much in Windows. Add execute "x" rights to all levels on all shell scripts in the current directory
chmod +x *.sh
ln Symbolic link
Make symbolic links between files. This is useful if you need to change the log directory or repair a symbolic link that was removed.
ln -s [path to new directory] ./retain-tomcat7
Network
wget Get File from website
Download the Latest Version of Retain
wget "http://download.gwava.com/download.php?product=Retain&version=current"
ifconfig
Find the IP address of the server. Use this command to find the IP address(es) and MAC address(es) the system uses. eth0 is the first NIC. If the system has multiple NICs, they are named, eth0, eth1, and so on. lo is loopback.
ifconfig
host
look up mx records
host -t MX [domain.com]
Determine hosts
cat /etc/resolv.conf
ping
Ping to see if a server is alive. Ctrl-c to stop.
ping [ip address]
DNS
DNS
nslookup server dig [servername or ipaddress]
Clear DNS Cache
rcnscd stop
then
rcnscd start
traceroute
Trace the route to a server
traceroute [domain.com]
hostname
Show the system name
hostname
ssh
Secure connect to another system, exit to leave ssh shell
ssh [hostname or ip address]
telnet
Useful for testing connections. You can send a test email with it:
telnet [IP address or DNS hostname] [port] ehlo [ENTER] mail from: retain@[your domain name] (note: the "from" can actually be spoofed - it really can contain any text of user@domain.com)[ENTER] rcpt to: [yours or a user's email address][ENTER] data[ENTER] subject: test email from telnet on Retain server[ENTER] This is a test[ENTER] .[ENTER] (you are literally typing a "period" here and pressing [ENTER]) quit[ENTER]
netstat
A useful tool for checking your network configuration and activity.
netstat -lnp -A inet
Process Commands
free Memory usage
Display memory usage
free -m
top System resouces
Display an overview of the system resources can be used to find a process
top
vmstat
Report virtual memory statistics: processes, memory, paging, block IO, traps, and cpu activity.
vmstat
chkconfig
Check runlevel of a process
chkconfig [process name]
ps aux Find a process
To find a running process you know the name of
ps aux | grep [process name]
kill End a process
You have to find a runaway process with top
or ps
kill [process ID]
It the process us hung you may have to be more forceful
kill -9 [process ID]
To kill all processes associated with a particualr name
killall -15 [processname]
w Who is logged in
Returns a list of logged in users if the TTY column shows "pts/3" that shows us that use is remotely logged in.
who w
fstab File system information
The fstab file will also contain information about any mounted filesystems as well
cat /etc/fstab
Linux Version
To find the version of Linux that is installed use this command:
cat /etc/*-release
Utilities
history Command history
Get a history of commands
history
To run a past command
![number of command to run from history]
To run a past command by name
![Ctrl-r][start typing desired command]
tar Create archives
Create an uncompressed tape archive of a bunch of files
tar -cvf [dest file name].tar [source directory]
Extract a tar
tar -xvf [filename]
zip/gzip Compress files
Zip
zip [dest file name] [source files or directory]
Unzip
unzip [file].zip -d /[directory]
Create a gzipped tar file
tar -czvf [dest file name] [source directory]
Extract a gziped tar
tar -xvzf [filename]
Extract gzip
gzip -dv [file name]
clear Clear Screen
Clear Screen
clear
crontab Schedule Tasks
crontab – Schedule a command to run at a later time.
Each line in the cron (cron is the daemon) table follows the following format: 7 fields left to right. Field Meaning 1 Minute (0-59) 2 Hour (2-24) 3 Day of month (1-31) 4 Month (1-12, Jan, Feb, ...) 5 Day of week (0-6) 0=Sunday, 1=Monday ... or Sun, Mon, Tue, Wed, Thur, Fri 6 User that the command will run as 7 Command to execute
So, if you want to have Netcleanse restart the ntp-client to reset the time every hour, then you'd use this command:
This will load the crontab editor. Add the entry below, save the change and exit the editor.
crontab -e
Restarts ntp-client every minute
0 * * * * root /etc/init.d/ntp-client restart
Runs the script at the top of every hour
00 */1 * * * /root/Desktop/gopostal/gopostalfullauto.sh
date Date and time
This command can be used to display the current time on the server or adjust the time.
date
rpm Package Installer
Package installer. There are numerous useful flags that can be used with this command. For those flags, look here: http://www.novell.com/coolsolutions/tip/19918.html Most commonly, however, you'll just use the commands to install, upgrade and uninstall.
rpm -ivh [filename].rpm
shutdown Shutdown and reboot
Shutdown the server
shutdown -r now init 6 reboot -f (Last resort reboot method. The only time you'd want to use this option is if something is wrong with the init daemon.)
vi Text Editor
vi – Text editor for Linux. There are a variety of commands you can use in vi, here are some of the most useful:
To open a file:
vi [filename]
- i: Insert text
- ESC: Exit mode, for example, exit the insert text mode
- /: Perform a search (n, repeats search; N, search backwards)
- dd: Deletes a line
- yy: Copies current line
- yw: Copies current word
- u: Undo last change
- p": Pastes whatever was copied after the cursor position
- wq: Writes changes and exits vi
- q!: Quits without saving changes
For more details on using vi, look here: [4]
less Pager program
Less lets you view the contents of a text file by screenfuls.
less [options] [filename]
Options:
- -g: Highlights just the current match of any searched string.
- -I: Case-insensitive searches.
- -M: Shows more detailed prompt, including file position.
- -N: Shows line numbers (useful for source code viewing).
- -S: Disables line wrap ("chop long lines"). Long lines can be seen by side scrolling.
- -?: Shows help.
- +F: Follow mode for log.
Once in less there are a number of commands that you can use:
- spacebar or page down: for next page
- page up or b: for previous page
- enter or j: for next line
- k: for previous line
- Shift-f: for follow mode. ctrl-c to abort follow mode.
- g or <: Return to first line
- G or >: Go to last line
- /[text]: Forward search for [text]
- ?[text]: Backwards search for [text]
- n: Next search match
- N: Previous search match
- ESCu: Turn off match highlighting (see -g option)
- h: Help. q to quit
- q: Quit