May 10, 2020
Estimated Post Reading Time ~

Useful UNIX Commands

CommandDescription
aliasAlias allows you to substitute a small or more familiar name in place of a long string.
Example: This usage will tailor the du command to use 1k units:
# alias du=du -k
awkAwk searches for patterns in a file and processes them. It enables a programmer to write small programs in the form of statements to make changes in text files when certain patterns appear or extract data from those files. This command simplifies a process historically done in C or Pascal languages.
Example: This will count the number of lines in the file – similar to wc –l
# awk ‘END{print NR}’
catAbbreviated from the word “concatenate,” which means to link things together, cat is used in Unix to link file contents and output them for viewing or printing. It is also used to display a file.
Example: This will view the /etc/hosts file.
# cat /etc/hosts
cdThe cd command sets the working directory of a process.
Example: This usage will change the directory to your home directory, assuming your HOME variable is defined
# cd $HOME
chmodChmod is a utility that changes the permission of a file.
Example: This will change the permission of the file called newfile to read, write and execute (rwx) for owner, group and other.
# chmod 777 newfile
chownA utility used to change file ownership.
Example: This usage will change the ownership of the file called newfile to frank.
# chown frank newfile
cpThe cp command is used to copy files.
Example: This usage will copy the file testdb to another directory on the system.
# cp /tmp/testdb /home/frank/testdb
cpioThis command is used to copy and move around files and directories. It also backs up empty directories which restores files from an archive or creates an archive.
Example: This will copy all files and directories in the current directory to another directory called /user/newfiles.
# find ./ -depth | cpio –pdmv /usr/newfiles
crontabCrontab is used for manipulating cron – used to schedule tasks.
Example: This usage displays your crontab file:
# crontab -l
dfThe df command reports filesystem disk space usage.
Example: This example displays disk space in kilobytes.
# df -k
duThe du command reports back the sizes of directory trees.
Example: This command reports summarizes the file size from the directory which you are running the command from.
# du –s
echoThis command lets you echo a string variable to standard output.
Example: This reports back “Hello World”
# echo Hello World
enableEnables or disables a printer.
Example: This enables printer1.
# enable printer1
envThe env command displays information about your current environment.
Example: This command displays all your environment variables
# env
exitAllows you to exit from a program, shell or UNIX network.
Example: This exits the shell.
# exit
exportExport sets the value of a variable so it is visible to all sub-processes that belong to the current shell.
Example: This command exports the TERM variable.
# TERM=vt220 ; export $TERM
exportfsThis command maintains a list of NFS exported file systems.
Example: This usage exports all directories
# exportfs -a
findFind searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.
Example: This command searches for all the files and directories in a given directory and sends them to standard output.
# find ./ -depth
ftp ftp allows for the copying of files back and forth on different host machines.
Example: This command starts up ftp
# ftp
grepGrep is a command used for searching one or more files for a given character string or pattern. It can also be used to replace the character string with another one.
Example: this usage searches for instances of hello within the file text.txt
# grep hello text.txt
gzipgzip is a compression utility designed to be a replacement for compress. Its main advantages over compress are much better compression and freedom from patented algorithms. It has been adopted by the GNU project and is now relatively popular on the Internet. gzip was written by Jean-loup Gailly and Mark Adler for the decompression code.
Example: This usage compresses the file and renames it to test2 in gzip format. gunzip is used to uncompress a gzip file.
# gzip -c testfile > test2.gz
headThis outputs the first part of a file
Example: This usage displays the first ten lines in the /etc/hosts file.
# head -10 /etc/hosts
idThis command prints out real and effective UIDs and GIDs.
Example: This usage shows all the uids and gids which show in your /etc/passwd.
# id
ifconfigifconfig checks a network interface configuration. It can be used, for example, to verify a user’s configuration if the user’s system has been recently configured or if the user’s system cannot reach the remote host while other systems on the same network can.
Example: This usage shows all the adapter information.
# ifconfig –a
ifdownShuts down the network interface.
Example: This usage brings down all interfaces which are up.
# ifdown -a
ifupStarts up a network interface.
Example: This usage brings up the interface en0
# ifup en0
lastThe last command shows the last users who logged into the system.
Example: This usage lists out the shutdown times and run-level changes
# last -x
lnThe ln command establishes links between files.
Example: This usage establishes a symbolic link between two files.
# ln -s sourcefile newfile
lsThe ls command shows information about files. With it, system administrators can list the contents of a directory in order to determine when the configurations files were last edited. There are many subcommands under ls, such as ls-r, which can reverse the order in which files are displayed. (The ls command is also discussed in this tip on troubleshooting tools.)
Example: This usage shows a long listing of file information in the data directory.
# ls –l data
manShort for “manual,” man unveils information about commands and a keyword search mechanism for needed commands. (The man command is also discussed in this tip on troubleshooting tools.)
Example: This usage prints out information on the tar command
# man tar
mvThis command moves a file to a different directory or filesystem.
Example: This usage moves the file test to the /tmp directory
# mv test /tmp
netstatThe netstat command shows the network status by symbolically displaying the contents of various network-related data structures. There are a number of output formats, depending on the options for the information presented.
Example: This usage shows the routing table.
# netstat –r
pingThe ping command (named after the sound of an active sonar system) sends echo requests to the host you specify on the command line, and lists the responses received their round trip time. When you terminate ping (probably by hitting control-C) it summarizes the results, giving the average round trip time and the percent packet loss. This command is used constantly to determine whether there is a problem with the network connection between two hosts.
Example: This usage sends echo requests to hosta.
# ping hosta
psThe ps command reports back process status. There are two ways that this is used – the System V way and the Berkeley method. The Berkeley method does not use the dash (-) before flags.
Example: This usage reports pack process info using the System V method
# ps –ef
pwdpwd is short for print working directory. The pwd command displays the name of the current working directory.
Example: This usage reports back the directory you are currently in
# pwd
rmThe rm command is used to remove a file or a group of files.
Example: This usage will prompt the user to make sure they want to delete the files in the directory before doing so
# rm –i *
rsyncrsync is focused on synching data from one disk location to another. It was created by Andrew Tridgell, one of Samba’s core team.
Example: This usage will transfer all files matching the pattern *.d from the current directory to the directory data on the machine hosta. (Learn more from this tip on Rsync and Amanda
# rsync *.d hosta:data/
sedsed (streams editor) isn’t really a true text editor or text processor. Instead, it is used to filter text, i.e., it takes text input and performs some operation (or set of operations) on it and outputs the modified text. Sed is typically used for extracting part of a file using pattern matching or substituting multiple occurrences of a string within a file.
Example: This usage inserts a blank line below every line which matches “test”.
# sed ‘/test/G’
shutdownShutdown is a command that turns off the computer and can be combined with variables such as -h for halt or -r for reboot.
Example: This usage reboots the box in AIX. Because the flags are a bit different between Unix flavors – do a man before running shutdown on your flavor.
# shutdown -Fr
sshSsh is used for secure network connections and tunneling of TCP services. OpenSSH is one open source SSH version.
Example: This usage connects user ken to hostb.com
# ssh -l ken hostb.com
sudoSudo (superuser do) allows a system administrator to give certain users (or groups of users) the ability to run some (or all) commands as root or another user while logging the commands and arguments.
Example: Assuming the user that is using sudo has already been configured to do so, this command allows him to run the adduser command as if he were root.
$ sudo -u root adduser testuser
tailThe tail command outputs the last part of a file.
Example: This usage displays the last ten lines of the /etc/hosts file.
# tail -10 /etc/hosts
tarThe tar program provides the ability to create tar archives, as well as various other kinds of manipulation. For example, you can use tar on previously created archives to extract files, store additional files, or update or list files. Initially, tar archives were used to store files on magnetic tape. The name “tar” comes from this use; it stands for “tape archiver.” Despite the utility’s name, tar can direct its output to available devices, files, or other programs. Tar may even access remote devices or files.
Example: This usage creates a tar archive to tape device st0 – with the contents of /home:
# tar -cvf /dev/st0 /home
traceroutetraceroute determines a route to the host and is very useful for distinguishing network/router issues. If the domain does not work or is not available you can traceroute an IP.
Example: This usage runs a traceroute to an inaccessible domain to try to determine the cause of the problem.
# traceroute hostc.org
unameuname prints system name and other related information about your system.
Example: This usage displays a long-listing of system information.
# uname -a
vivi is a screen-based editor preferred by the vast majority of Unix users.
Example: This usage brings up vi in editor mode – to edit the /etc/hosts file.
# vi /etc/hosts
vmstatThe vmstat command is used to get a snapshot of everything going on in the system – helping systems administrators determine whether the bottleneck is CPU, memory or I/O. Run this command to get virtual memory statistics.
Example: This usage starts up vmstat and runs it every two seconds for ten iterations.
# vmstat 2 10
wThe w command prints a summary of current system and user information, while also letting you know who is logged in to the system and what they are doing.
Example: this usage prints out a short version of information.
# w –s
whoThe who command displays the users who are logged on to the system.
Example: This usage lists all the available output of the who command for each user.
# who –a
whoamiThe whoami command displays who the user is that you are logged on as. This is very useful for those of you that have different logins.
Example: This usage displays the name of the user that you are presently logged in as.
# whoami


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.