Moving around the File System

In Linux, fi les are organized within a hierarchy of directories. Each directory can contain fi les, as
well as other directories. You can refer to any fi le or directory using either a full path (for example,
/home/joe/myfile.txt) or a relative path (for example, if /home/joe were your current
directory, you could simply refer to the fi le as myfile.txt).

Dir Func
/bin Contains common Linux user commands
/boot Has the bootable Linux kernel and boot loader confi guration fi les (GRUB)
/dev Contains fi les representing access points to devices on your systems
/etc Contains administrative configuration files
/home Contains directories assigned to each regular user with a login account
/media Provides a standard location for automounting devices
/lib Contains shared libraries needed by applications in /bin and /sbin to boot the system
/mnt A common mount point for many devices before it was supplanted by the standard /media directory
/misc A directory sometimes used to automount filesystems upon request
/opt Directory structure available to store add-on application software
/proc Contains information about system resources
/root Represents the root user’s home directory
/sbin Contains administrative commands and daemon processes
/tmp Contains temporary fi les used by applications
/usr Contains user documentation, games, graphical files (X11), libraries (lib),and a variety of other commands and files that are not needed during the boot process
/var Contains directories of data used by various applications

Using Basic Filesystem Commands

Command Result
cd Changes to another directory
pwd Print the name os the current working directory
mkdir Creates a directory
chmod Changes the permission on a file or directory
ls Lists the contents of a directiory

Using Metacharacters and Operators

Using fi le-matching metacharacters

[COMMAND ls]

$ touch apple banana grape grapefruit watermelon
$ ls a*
apple
$ ls g*
grape grapefruit
$ ls g*t
grapefruit
$ ls *e*
apple grape grapefruit watermelon
$ ls *n*
banana watermelon
$ ls ????e
apple grape
$ ls g???e*
grape grapefruit
$ ls [abw]*
apple banana watermelon
$ ls [agw]*[ne]
apple grape watermelon
$ ls [a-g]*
apple banana grape grapefruit

Using file-redirection metacharacters

echo "I finished the project on $(date)" >> ~/projects

Using brace expansion characters

[COMMAND touch]

$ touch memo{1,2,3,4,5}
$ ls
memo1 memo2 memo3 memo4 memo5
$ touch {a..f}{1..5}
$ ls
a1 a3 a5 b2 b4 c1 c3 c5 d2 d4 e1 e3 e5 f2 f4
a2 a4 b1 b3 b5 c2 c4 d1 d3 d5 e2 e4 f1 f3 f5

List Files and Directories

ubuntu@ip-172-31-24-188:~$ ls
git  setup.sh  There
ubuntu@ip-172-31-24-188:~$ ls -l
total 16
drwxrwxr-x 3 ubuntu ubuntu  4096 Jan 16 17:03 git
-rw-rw-r-- 1 ubuntu ubuntu 10338 Jan 17 07:00 setup.sh
-rw-rw-r-- 1 ubuntu ubuntu     0 Jan 19 02:33 There
ubuntu@ip-172-31-24-188:~$ ls -al
total 64
drwxr-xr-x 7 ubuntu ubuntu  4096 Jan 19 02:33 .
drwxr-xr-x 4 root   root    4096 Jan 16 17:43 ..
-rw------- 1 ubuntu ubuntu  1012 Jan 19 05:13 .bash_history
-rw-r--r-- 1 ubuntu ubuntu   220 Apr  9  2014 .bash_logout
-rw-r--r-- 1 ubuntu ubuntu  3637 Apr  9  2014 .bashrc
drwx------ 2 ubuntu ubuntu  4096 Jan 16 17:01 .cache
-rw-rw-r-- 1 ubuntu ubuntu     0 Jan 16 17:01 .cloud-locale-test.skip
drwxrwxr-x 6 ubuntu ubuntu  4096 Jan 18 08:05 .cpan
drwxrwxr-x 3 ubuntu ubuntu  4096 Jan 16 17:03 git
-rw-rw-r-- 1 ubuntu ubuntu    48 Jan 16 17:09 .gitconfig
drwx------ 2 ubuntu ubuntu  4096 Jan 18 05:36 .irssi
-rw-r--r-- 1 ubuntu ubuntu   675 Apr  9  2014 .profile
-rw-rw-r-- 1 ubuntu ubuntu 10338 Jan 17 07:00 setup.sh
drwx------ 2 ubuntu ubuntu  4096 Jan 16 17:29 .ssh
-rw-rw-r-- 1 ubuntu ubuntu     0 Jan 19 02:33 There
-rw------- 1 ubuntu ubuntu  1285 Jan 16 17:29 .viminfo

Understanding File Permissions and Ownership

Permission File Directory
Read View what's in the file See what files and subdirectories it contains
Write Change the file's content,rename it or delete it Add or Remove files and subdirectories to th directory
Execute Run the file as a program Change to the directory as the current directory, search through the directory, or execute a program from the directory. Access fi le metadata of fi les in that directory.

Changing permissions with chmod (numbers)

In one method of doing this, each permission (read, write, and execute) is assigned
a number—r=4, w=2, and x=1—and you use each set’s total number to establish the
permission. For example, to make permissions wide open for yourself as owner, you would set the fi rst number to 7 (4+2+1), and then you would give the group and others read-only permission by setting both the second and third numbers to 4 (4+0+0), so that the final number is 744.
Here are some examples of how to change permission on a fi le (named file) and what the resulting permission would be:
The following chmod command results in this permission: rwxrwxrwx

chmod 777 file  

The following chmod command results in this permission: rwxr-xr-x

chmod 755 file  

The following chmod command results in this permission: rw-r--r--

chmod 644 file  

The following chmod command results in this permission: ---------

chmod 000 file    

Changing permissions with chmod (letters)

You can also turn fi le permissions on and off using plus (+) and minus (–) signs,
respectively, along with letters to indicate what changes and for whom. Using letters, for each fi le you can change permission for the user (u), group (g), other (o), and all users (a). What you would change includes the read (r), write (w), and execute (x) bits. For example,start with a fi le that has all permissions open (rwxrwxrwx).

The resulting permissions are shown to the right of each command:
[COMMAND chmod]
The following chmod command results in this permission: r-xr-xr-x

chmod a-w file  

The following chmod command results in this permission: rwxrwxrw-

chmod o-x file  

The following chmod command results in this permission: rwx------

chmod go-rwx file  

Likewise, the following examples start with all permissions closed (---------). The plus sign is used with chmod to turn permissions on:
The following chmod command results in this permission: rw-------

chmod u+rw files  

The following chmod command results in this permission: --x--x--x

chmod a+x files  

The following chmod command results in this permission: r-xr-x---

chmod ug+rx files  

Changing file ownership

[COMMAND chown]

# chown joe /home/joe/memo.txt
# ls -l /home/joe/memo.txt
-rw-r--r--. 1 joe root 0 Dec 19 11:23 /home/joe/memo.txt
# chown joe:joe /home/joe/memo.txt
# ls -l /home/joe/memo.txt
-rw-r--r--. 1 joe joe 0 Dec 19 11:23 /home/joe/memo.txt
# chown -R joe:joe /media/myusb

[COMMAND chgrp]

sudo chgrp -R root gogs/ gogs-repositories/

Moving, Copying, and Removing Files

[COMMAND mv]
[COMMAND cp]
[COMMAND rm]
[COMMAND rmdir]

$ mv abc def
$ mv abc ~
$ mv /home/joe/mymemos/ /home/joe/Documents/

$ cp abc def
$ cp abc ~
$ cp -r /usr/share/doc/bash-completion* /tmp/a/
$ cp -ra /usr/share/doc/bash-completion* /tmp/b/

$ rm abc
$ rm *
$ rmdir /home/joe/nothing/
$ rm -r /home/joe/bigdir/
$ rm -rf /home/joe/hugedir/