Files & Directories
In linux, everything is a file or directory. All files and directories have a path. There is a relative and a full path, with relative being a path relative to the current working directory.
To see your current working directory, run the pwd
(Print Working Directory) command.
$ pwd
/home/sysadmin
Listing Files and Directories
The ls
(LiSt) command lists the contents of a directory. If no arguments
are specified, it lists the contents of the current directory.
$ ls
development
Documents
Downloads
Pictures
$ ls Pictures
picture-of-a-cat.png
picture-of-a-dog.jpg
Files that begin with a .
are "hidden" files, and do not normally appear
the output of an ls
command. To show all files, use the -a
(All) flag.
$ ls -a
.bash_history
.bash_logout
.bash_profile
.bashrc
.profile
.vimrc
development
Documents
Downloads
Pictures
Changing Your Working Directory
The cd
command changes your working directory to the path you specify.
Paths can be relative or absolute.
Absolute Paths
Absolute paths are paths that begin from /
(root).
$ cd /home/sysadmin/Documents
$ # or
$ cd ~/Documents
Note: The
~
(tilde) character is a shortcut for the current user's home directory.
Relative Paths
Relative paths are paths relative to the current working directory.
The simplest relative path is a child directory of the current working directory.
$ ls
Documents
Pictures
$ cd Documents
$ ls
Guide.pdf
Resources.txt
.
is the current directory.
$ pwd
/home/sysadmin
$ cd .
$ pwd
/home/sysadmin
..
is the parent directory.
$ pwd
/home/sysadmin/Pictures
$ cd ..
$ pwd
/home/sysadmin
And more complex...
$ pwd
/home/sysadmin/Pictures
$ cd ../Documents
$ pwd
/home/sysadmin/Documents
Copying and Moving Files & Directories
Copying
Use the cp
(CoPy) command to copy files and directories.
$ cp <source> <destination>
To copy a directory AND its contents, use the
-r
(Recursive) flag.
$ cp -r <source directory> <destination directory>
Moving
Moving files and directories works much the same with the mv
(MoVe) command.
$ mv <source> <destination>
You can also use the mv
command to rename a file or directory
$ mv <oldname> <newname>
Links
A link works like a file to point from one place to another. There are two types of links:
Hard Links
A hard link is a file that points to the inode
of another file. If this
"pointed-to" file is deleted, the data persists until all hard links are
removed (a normal file acts as a hard link to itself). A hard link can only
be created for files, not directories.
Create a hard link with the ln
(LiNk) command.
ln <source> <destination>
$ cat srcFile
hello i am a file.
$ ln srcFile hardLink
$ rm srcFile
$ cat hardLink
hello i am a file.
In the permissions section, we look at the ls -l
command. We can see the number of hard links to a file in the output.
$ ls -l
-rw-rw-r-- 1 sysadmin users 0 Dec 1 2021 srcFile
-rw-r--r-- 1 sysadmin users 584 Mar 10 2022 script.sh
The number before sysadmin is the number of hard links.
$ ln srcFile hardLink
$ ls -l
-rw-rw-r-- 2 sysadmin users 0 Dec 1 2021 hardlink
-rw-rw-r-- 2 sysadmin users 0 Dec 1 2021 srcFile
-rw-r--r-- 1 sysadmin users 584 Mar 10 2022 script.sh
As we can see here, after adding a hard link, the number of hard links increases for the source file, and the number is reflected in the hard link too.
Soft/Symbolic Link
A symbolic link (symlink for short) is a file that points to another file or directory's path. If the "pointed-to" file or directory is deleted, the link is no longer valid, and points to a non-existent file or directory.
The -s
(Soft) flag specifies a symbolic link.
To create a link called newLink to a file called srcFile, run the following command:
$ ln -sf srcFile newLink
$ cat srcFile
this is the source file
$ ln -sf srcFile newLink
$ cat newLink
this is the source file
The
cat
(conCATenate) command allows you to read the contents of a file.