Basic Linux Commands

Basic stuff

cat filename.txt Print the contents of filename.txt to your screen
cd - Change to the last visited directory
cd .. Cahnge one directory up
cd dirname Change into directory "dirname"
cd ~   or  cd  Change into your home directory
chmod Changes file access permissions:  USER - GROUP - EVERONE

0 = --- No permission
1 = --x  Execute only
2 = -w-  Write only
3 = -wx  Write and execute
4 = r-- Read only
5 = r-x Read and execute
6 = rw-  Read and write
7 = rwx Read, write and execute

chmod 000 filename No one can access
chmod 644 filename Usually for text files
chmod 755 filename Usually for executable scripts/files
chown root myfile.txt Changes the owner of the file to root [requires root privileges].
chown owner:group  myfile.txt Changes the owner and group of the file to new owner and "group" group [requires root privileges].
cp -a /etc/* /root/etc/ Copies all files, retaining permissions form one directory to another.
cp -av *  /path/to/newdirectory Copies all files and directories recurrsively in the current directory into newdirectory
cp filename filename.bak Copies filename to filename.bak
file * Prints out a list of all files/directories in a directory
file Attempts to guess what type of file a file is by looking at it's content.
find / -name "filename" Finds the file called "filename" on your filesystem starting the search from the root directory "/".
find Utility to find files / directories on a computer.
grep   Looks for patterns in file(s)
grep word filename.txt Shows all matches of word "word" in file filename.txt
grep -v word filename.txt Shows all lines that do not match root
gv View Postscript/PDF files
less Similar to more command
ln -s /home/user/file1 /home/user/file2 Now you can edit /etc/httpd.conf rather than the original. changes will affect the orginal, however you can delete the link and it will not delete the original.
ln Create's "links" between files and directories
locate filename Finds the file name and path of which contains the string "filename". Run 'updatedb' to build index.
ls -la Shows all files (including ones that start with a period), directories, and details attributes for each file.
ls List files in current directory.
more filename Print content of file "filename" screen by screen. Use space bar to procceed to the next page, q is to quit.
mv Move a file command
mv oldfilename newfilename Move a file or directory from oldfilename to newfilename
nl Number lines
od View binary files and data
rm -f filename.txt Deletes filename.txt, will not ask for confirmation before deleting.
rm -rf tmp/ Recursively deletes the directory tmp, and all files in it, including subdirectories.
rm Deletes a file
rm -i filename.txt  Will ask if you really want to delete it
stat Display file attributes
tail -100 filename Print the last 100 lines of the file to the screen
tail -f filename Watch the file continuously, while it's being updated
tail filename See the last 20 (by default) lines of filename
touch Create an empty file
touch /home/user/filename.txt Creates an empty file "filename.txt" in the directory /home/user/
wc -l filename.txt Counts # of lines in filename.txt
wc Counts # of words
xdvi View TeX DVI files
xxd Also view binary files and data

Editors

pico,    vi,    emacs The most popular editors
vi    filename.txt Edit filename.txt. All commands in vi are preceded by pressing the escape key. Each time a different command is to be entered, the escape key needs to be used. Except where indicated, vi is case sensitive. 

H --- Upper left corner (home)
M --- Middle line
L --- Lower left corner
h --- Back a character
j --- Down a line
k --- Up a line
^ --- Beginning of line
$ --- End of line
l --- Forward a character
w --- Forward one word
b --- Back one word
fc --- Find c
; --- Repeat find (find next c)

:q! --- This force quits the file without saving and exits vi
:w --- This writes the file to disk, saves it
:wq --- This saves the file to disk and exists vi
:$ --- Takes you to the last line of the file
:0 --- Takes you to the first line of the file

See quick reference quide in txt form.

emacs  -nw filename.txt C-\ t --- Tutorial suggested for new emacs users.
C-x C-c exit emacs

C-x C-f --- read a file into emacs
C-x C-s --- save a file back to disk

C-f --- move forward one character
C-b --- move backward one character
C-n --- move to next line
C-p --- move to previous line
C-a --- move to beginning of line
C-e --- move to end of line
M-f --- move forward one word
M-b --- move backword one word
C-v --- move forward one screen
M-v --- move backward one screen
M-< --- go to beginning of file
M-> --- go to end of file  

See quick reference quide in txt form or pdf form.

Network commands 

arp Command mostly used for checking existing Ethernet connectivity and IP address
ifconfig -a Display into on all network interfaces on server, active or inactive..
ifconfig Display info on the network interfaces.
netstat -an Shows all connections to the server, the source and destination ips and ports.
netstat -an |grep :80 |wc -l Show how many active connections there are to apache (httpd runs on port 80)
netstat -rn Shows routing table for all ips bound to the server.
netstat Shows all current network connections.
nslookup beowulf.rutgers.edu Query your default domain name server (DNS) for an Internet name (or IP number) host_to_find.
ping beowulf.rutgers.edu Sends test packets to a specified server (beowulf.rutgers.edu in this example) to check if it is responding properly
route -n Shows routing table for all ips bound to the server.
tcpdump Print all the network traffic going through the network [requires root privileges]..
top -u root Show processes running by user root only.
top Shows live system processes in a formatted table, memory information, uptime and other useful info. While in top, Shift + M to sort by memory usage or Shift + P to sort by CPU usage
traceroute beowulf.rutgers.edu Have a look how you messages travel to yahoo.com
w Shows who is currently logged in and where they are logged in from.
who This also shows who is on the server in an shell.

System commands

date Print or change the operating system date and time
dmesg | less Print kernel messages.
du -sh Shows a summary of total disk space used in the current directory, including subdirectories.
du dirname -bh | more Print detailed disk usage for each subdirectory starting at the "dirname".
du Shows disk usage.
free Memory info (in kilobytes).
fsck Check a disk for errors [requires root privileges].
hostname Print the name of the local host. Use netconf (as root) to change the name of the machine.
kill -9 PID Immediately kill process ID.
kill Terminates a system process.
killall program_name Kill program(s) by name. For example to kill instances of httpd, do 'killall bash'.
last -10 -a Shows last 10 logins, with the hostname in the last field.
last -10 Shows only the last 10 logins.
last Shows who logged in and when.
lsmod Show the kernel modules currently loaded [requires root privileges].
man topic Display the contents of the system manual pages (help) on the topic. Do 'man netstat' to find all details of netstat command including options and examples.
mount -t auto /dev/cdrom /mnt/cdrom Mount the CD. The directory /mnt/cdrom must exist [often requires root privileges].
mount -t auto /dev/fd0 /mnt/floppy Mount the floppy. The directory /mnt/floppy must exist [often requires root privileges].
mount Mount local drive or remote file system [requires root privileges].
ps U username Shows processes for a certain user.
ps auxf Shows all system processes like the above but organizes in a hierarchy that's very useful.
ps aux Shows all system processes.
ps ps is short for process status, which is similar to the top command. It's used to show currently running processes and their PID. A process ID is a unique number that identifies a process, with that you can kill or terminate a running program on your server (see kill command).
pwd Print working directory, i.e., display the name of my current directory on the screen.
reboot  Reboot the machine [requires root privileges].
sudo The super-user do command that allows you to run specific commands that require root access.
time Determine the amount of time that it takes for a process to complete.
uname -a Displays info on about your server such as kernel version.
uptime Show the number days server has been up including system load averages.
whoami Print  login name.

Compression commands

bzip2 Compress files in bzip2 format
compress Compress files. Compress filename end with .Z
gzip  filename Compresses file with name filename and produces file with name filename.gz
gzip -d   filename.gz Uncompresses file filename.gz and produces file filename.
tar -cf archive.tar dirname Takes everything from directory dirname  and puts it into archive.tar
tar -xvf file.tar Extracts the files
tar -zxvf file.tar.gz Extracts the files from compressed archive
tar Creating and Extracting .tar.gz, .tgz and  .tar files. See man tar
uncompress Uncompress compressed files. Uncompress filename.Z
unzip file.zip Extracting .zip files shell command
zip Compress files into.zip
rpm2cpio pack.rpm |cpio -ivd To extract contend of  RPM package with name pack.
[cpio command : -i -- extract, -d --  make local subdirectories, -v -- verbose]
rpm2cpio pack.rpm |cpio -t To lists all the files in the pack package
rpm -qa To list all RPM packages installed.
rpm -e To remove a package.
rpm -Uhv To install a package.

Hidden  configurations files

.bash_history The list of  executed commands.
.bash_login Treated by bash like .bash_profile if that doesn't exist.
.bash_logout Sourced by bash login shells at exit.
.bash_profile Sourced by bash login shells after /etc/profile
.emacs Read by emacs at startup
.profile Treated by bash like ~/.bash_profile if that and .bash_login don't exist.
.vimrc Default vi-editor  configuration file.

Configuration files

etc This directory contains most of the basic Linux system-configuration Files.
/etc/cron* Directories in this set contain files that define how the crond utility runs applications on a daily (cron.daily), hourly (cron.hourly), monthly (cron.monthly), or weekly (cron.weekly) schedule.
/etc/crontab   examples Minute (0-59) Hour (0-23) Day of Month (1-31) Month (1-12 or Jan-Dec) Day of Week (0-6 or Sun-Sat) Command
30 2 12 * 0,6 command 
At 2:30AM on the 12th of every month that a Sunday or Saturday falls on.
0 0,12 1 */2 * command 
At 12am and 12pm on the 1st day of every 2nd month.
0 2 1-10 * * command
At 2am on the 1st thru the 10th of each month.
30 12 * * 1,3,5 command
At 12:30PM  every day, every month, on Mon, Wed, Fri
/etc/cups Contains files used to configure the CUPS printing service.
/etc/default Contains files that set default values for various utilities. For example, the file for the useradd command defines the default group number, home directory, password expiration date, shell, and skeleton directory
/etc/init.d Contains the permanent copies of System V–style run-level scripts. These scripts are often linked to files in the /etc/rc?.d directories to have each service associated with a script started or stopped for the particular run level. The ? is replaced by the run-level number (0 through 6). (Slackware puts its run-level scripts in the /etc/rc.d directory.)
/etc/mail Contains files used to configure your sendmail mail service.
/etc/passwd Holds some user account info including passwords (when not "shadowed").
/etc/security Contains files that set a variety of default security conditions for your computer.
/etc/shadow Contains the encrypted password information for users' accounts and optionally the password aging information.
/etc/skel Any files contained in this directory are automatically copied to a user’s home directory when that user is added to the system.
/etc/sysconfig Contains important system configuration files that are created and maintained by various services (including iptables, samba, and most networking services).
/etc/syslogd.conf The configuration file for the syslogd daemon. syslogd is the daemon that takes care of logging (writing to disk) messages coming from other programs to the system.
/etc/xinetd.d Contains a set of files, each of which defines a network service that the xinetd daemon listens for on a particular port.
/var Contains variable data like system logging files, mail and printer spool directories, and transient and temporary files.
/var/log Log files from the system and various programs/services, especially login (/var/log/wtmp, which logs all logins and logouts into the system) and syslog (/var/log/messages, where all kernel and system program message are usually stored).
/var/log/messages System logs. The first place you should look at if your system is in trouble.
/var/log/utmp Active user sessions. This is a data file and as such it can not be viewed normally.
/var/log/wtmp Log of all users who have logged into and out of the system. The last command can be used to access a human readable form of this file.


Copyright © 2024, HPC, RUPC, Rutgers, The State University of New Jersey.
Wednesday, 26-Feb-2025 08:10:41 EST