Unix/Linux Basic Commands

Getting Help
• man
• info
• apropos – search for only exact matches
• whatis – to list a one−line description of a program
• Software packages may store its help files in /usr/share/doc
• Online help: SuperMan Pages, Linux Documentation Project (LDP)
• LinuxQuestions.org

Handy shortcuts
# up(down)_key scrolls through command history
# <something-incomplete> TAB completes path/file_name
# Ctrl+a # cursor to beginning of command line
# Ctrl+e # cursor to end of command line
# Ctrl+d # delete character under cursor
# Ctrl+k # cut line from cursor into kill buffer
# Ctrl+y # paste content from Ctrl k

Managing Files and Directories
Files and Directories Commands
• pwd
• cd
• ls

File types that may be listed by ls –l :
(see the options in the example below)
– regular file
d directory
l symbolic link
b block special file
c character special file
p named pipe
s socket

• cp – for copying between hosts, see next section
• rm
• mv
• mkdir
• rmdir
• touch – create empty files or update file timestamps

cd .. # one level up
cd    # home directory
cd –  # previous directory

ls –a # include hidden files
ls –l # long listing
ls –R # recurses through subdirectories with contents
ls –d # directory names without their contents
ls –lh # print sizes in human readable format
ls –ld # avoid listing directory contents
ls –i # print index number
ls –S # sort by file size
ls –t # sort by modification time (newest first)
ls –r # reverse order while sorting
ls –l –time-style=STYLE # STYLE: full-iso, long-iso, iso, locale, +FORMAT

cp file1 file2 # timestamp changes for the new file
cp –p file1 file2 – # all of the attributes are preserved
cp file1 dir1
cp file1 file2 dir1
cp ./dir1/* dir2
cp –r dir1 dir2 # -r (same as -R) copy entire directory tree
# links aren’t copied, permissions aren’t preserved
cp –a dir1 dir2 # copy the entire tree including permissions and links

mv file1 file2 # renames directories or files
mv file1 ./dir1 # moves file/directory as specified in path

rm file1 # removes file name
rm -r dir1 # removes directory including its content,
rm -rf dir # ‘f’ argument turns confirmation off
rm — -myfile # the fielename containes hyphen

touch {doc,sheet}_{jan,feb,mar}
touch file{0..6}.txt

Determining File Content
• file
Viewing Files
• cat
• less

less navigation commands:
space ahead one full screen
ENTER ahead one line
b back one full screen
k back one line
g top of the file
G bottom of the file
/text search forward for text (Regular Expressions can be used)
n repeat last search
N repeat backward last search
q quit

Hard and Symbolic (Soft) Links
• ln
• ls –l in case of soft link, it displays the link name and the referenced file

Checking Free Space
• df space usage by file systems
• du disk space by directories and subdirectories
df –h # -h prints size in readable format
du –h –s ~ # -s reports single directory summary

Searching Files By Names
• locate [options] name(s)
• slocate [options] name(s)
• Only the files you own are searched
• Some options are shown in the example below.
• locate.db or slocate.db databases are used
• updatedb or locate -u

to manually update the database
locate “*.png” # wildcard characters can be used
locate “*.png” -q # supress errors
locate -n 15 “*.html” # only 15 resultes returned
locate -i “*.HtmL” # case-insensitive search

Searching Files By Names and Attributes
• find <dirs> [conditions] [–exec cmd {} ;]

-atime n File was last accessed n days ago
-ctime n File was last changed n days ago
-user uname File is owned by user uname (or user ID)
-group gname File belongs to group gname (or group ID)
-size n[cwbkMG]b 512-byte blocks (default), c in bytes, w two-byte words, k kilobyte
-iname case -insensitive version of –name
-o logical operator between criteria (by default it is AND)
-not negate (logical NOT)
-perm mode permission bits are exactly mode (octal or symbolic).
-perm -mode ALL of the permission bits mode are set for the file.
Any of the permission bits mode are set for the file.
-perm +mode
-regex pattern Full path filename (not only filename) matches regular expression
-mtime n Files was last modified Exactly n*24 hours ago.
-mtime +n Files was last modified >= n*24 hours ago.
-mtime -n Files was last modified <= n*24 hours ago.
-mmin n Files was last modified n minutes ago.
−daystart measure time in the options above from the beginning of the current
day instead of 24 hours ago.
-newer <file> Files newer than <file> modification date

find . –name “*.html”
find -iname snow.png
find -user peter -group peter
find -user joe -not -group joe
find -user joe -o -user jane
find -not ( -user joe -o -user jane )
find -perm 755
# matches if mode is exactly 755
find -perm +222 # matches if anyone can write
find -perm -222 # matches if everyone can write
find -perm -002 # matches if other can write
find -size 1024k
# excatly 1 MB
find -size +1024k # over 1 MB
find -size -1024k # less than 1 MB
find ~ −empty
# find empty regular files or directories
find -size +102400k -ok gzip {} ; # OK prompte before acting
find . -regex ‘.*[124].*ms$’

Archiving Files
• tar cvf archive_name files …
to create an archive file
c create a new archivd
v produces verbose messages
f archive file name
j use bzip2 compression
z use gzip compression
• tar tf archive_name to inspect files in an archive, if v option is used, long file list
• tar xvf archive_name to extract an archive file (always to current directory)

Compression Utilities
• gzip –v file(s)
v option displays compression percentage, original file replaced
only regular files are compressed
• bzip2 –v file
better compression
• gunzip filename.gz uncompress the file
• gzip –d filename.gz uncompress the file
• gunzip –c filename.gz list contents of the compressed file in STDOUT, the file
• bunzip2 –v file

Users, Groups and Permissions

Change Password
• passwd username
Change Your Identity

• su username
• su – username
# start a login shell
User Information Commands
• whoami # who you are
• groups, id # what groups you belong to
• users, who, w # who is logged on
• last # login/reboot history
Changing File Ownership

• chown user_name file|directory
• chgrp group_name file|directory

Changing Permissions
• chmod mode file
where mode is: [u,g or o] [+ or -] [r, w or x] (Symbolic Method)
where mode is: 4:r 2:w 1:x (Numeric Method)

chmod o-rwx file1
chmod u-w,go-x file1
chmod +x file1 # the file is executable to all security levels
chmod 775 file1

Post navigation

Arunlal Ashok

SRE at NewFold Digital. Linux lover. Traveller. Father of two princesses.

Always happy for an open discussion! Write to arun ((@)) crybit ((dot)) com.

One thought on “Unix/Linux Basic Commands

Leave a Reply

Your email address will not be published. Required fields are marked *