Skip to content

Unix Basics

mianahom edited this page Jan 20, 2023 · 10 revisions

Unix/Linux Command Line Basics

Notes

  • Unix/Linux command line is case sensitive
  • the hash/pound sign (#) indicates end of a command the start of a comment
  • the notation <...> refers to variables and file names that need to be specified by the user. The symbols < and > need to be excluded

Orientation

Viewing and Changing the present working directory

pwd           # get the full path of the present working directory
ls            # list content of pwd
ls -l         # similar to ls, but provides more information on files and directories
ls -a         # includes hidden files (.name) as well
ls -R         # lists subdirectories recursively
ls -t         # lists files in chronological order
cd <dir_name> # brings you to the indicated directory
cd ~          # brings you to your home directory
cd ..         # moves you one directory up
cd ../..      # moves two directories up (and so on)
cd -          # go back to where you were before the last directory change

The tilde symbol (~) is the path to your home directory

Period/dot (.) refers to the present working directory.

This will happen anywhere on the command line:

echo ~        # view the complete path of your home (similar to pwd)
find ~        # list all your files (including everything in sub-directories)
ls ~          # list the top level files and directories of your home directory
ls .          # list the files and directories in the directory you are currently in
du -sch ~/*   # calculate the file sizes in your home

Viewing information: file, user, host, space, memory, etc:

stat <file-name>       # Last modification time stamps, permissions, and size of a file
hostname               # shows on which machine you are (same as "echo $HOSTNAME")
df                     # disk space
free -g                # memory info in Megabytes
uname -a               # shows tech info about machine
/sbin/ifconfig         # give IP and other network info
du -sh                 # displays disk space usage of current directory
du -sh *               #displays disk space usage of individual files/directories
du -s * | sort -nr     # shows disk space used by different directories/files sorted by size

Files and Directories

mkdir <dir_name>    # creates specified directory
rmdir <dir_name>    # removes an empty directory
rm <file_name>      # removes file
rm -r <dir_name>    # removes directory including its content, but asks for confirmation, 'f' argument turn confirmation off
cp <name> <path>    # copy file/directory as specified in path (-r to include content in directories)
mv <name1> <name2>  # renames directories or files
mv <name> <path>    # moves file/directory as specified in path

Text Viewing

more <file>          # views text, use space bar to browse and q to text
less <file>          # more versatile text viewer than 'more'
                     # q to exit
                     # G to jump to end of text
                     # g to go to beginning
                     # / find forward
                     # ? find backwards
cat <file> <output>  # concatenates files and prints content to standard output
head -<number> <file> # prints first lines of a file
tail -<number> <file> # prints last lines of a file

File Manipulation

cat <file1> <file2> <file.out> # concatenate files in output file
paste <file1> <file 2> <paste.out> #

Handy Shortcuts

  • Using the up arrow and down arrow will scroll through command history
  • history shows all commands you have used recently
  • The tab key will auto complete a file/directory
  • | execute two commands together with a pipe ( | )
  • control shortcuts in command line
    ctrl+a # cursor to beginning of command line
    ctrl+e # cursor to end of command line
    ctrl+w # cut last word
    ctrl+k # cut from cursor to end of the line
    ctrl+y # paste content that was cut by ctrl+w or ctrl+k

Unix Help

man <something> # general help
man wc          # manual on program 'word count' wc
wc --help       # short help on wc
soap -h         # for less standard programs

Useful Unix Commands

For more on universally available Linux commands with detailed examples and explanations: http://www.linuxconfig.org/linux-commands

Finding Things

Finding files, directories, applications

find <location to search> -options <what to find> 
find . -name *.txt           # find files in the current directory that end in .txt
find . -iname *.txt          # same as above, but case insensitive
find ~ -type f -mtime -2     # find all files you have modified in the last two days
# other useful arguments : -user <user name>, -group <group name>, -ctime <number of days ago changed> 
locate <pattern>             # finds files and directories that are written into update
which <application_name>     # find location of application
whereis <application_name>   # searches for executables in set of directories
dpkg -l | grep <mypattern>   # find Debian packages and refine search with grep pattern

Finding things in files, extracting text

grep

grep <pattern> <file>    # provides lines in file where pattern appears
grep -H <pattern>        # -H prints out file name in front of pattern 
grep <'pattern'> file    # if pattern is shell function uses single quotes
grep <pattern> file | wc # pipes line with pattern into word count

wc

wc <filename>      # output shows number of lines, words, bytes
wc -l <filename>   # print number of lines in a file
wc -w <filename>   # print number of words in a file
wc -c <filename>   # display the count of bytes in a file
wc -m <filename>   # print the count of characters from a file
wc -L <filename>   # print only the length of the longest line in a file

sed

sed 's/<original>/<new>/' <file>  # replace first instance of "original" on each line of file with "new"
sed 's/<original>/<new>/g' <file> # replace all instances of "original" with "new" in file

Indicate a pattern or Extract a basename

*               # the asterisk will represent any characters in a file name
*.txt           # all files that end in .txt
sample1*        # all the files that begin with sample 1
* *             # all the files with a space in the name
\               #
basename sample1.fastq.gz fastq.gz      #

Permissions and Ownership

  • rwx: read write execute
  • when listed in triplet (ex. drwxrwxrwx)
    • d: directory
    • first triplet: user permissions (u)
    • second triplet: group permissions (g)
    • third triplet: world permissions (o)
  • (+) to add permissions
  • (-) to remove permissions
  • (=) to make permissions specified the only permissions that file has
ls -al                                   # list information for each file in the directory
chmod ug+rx <file>                       # give user and group read and execute permissions
chmod ugo-rwx <file>                     # remove all permissions from all users of group
chown <user> <file or directory>         # changes user ownership
chgrp <group> <file or directory>        # changes group ownership
chown <user>:<group> <file or directory> # changes user and group ownership