Day 12: Linux File System #90DaysofDevOps
Ayushi Tiwari
Java Software Developer | Certified Microsoft Technology Associate (MTA)
An Introduction to the Linux File System
Linux has a directory-based structure. Files are stored within a particular directory, which is also referred to as a folder. Each directory can also contain other directories. These directories can in turn contain files and directories.
Conceptually, this can be thought of in terms of a tree structure. At the trunk of the tree is the / directory, also known as the root directory. All other directories branch off of the root directory. The root file system contains all the files and directories which are on the same disk partition as the root directory. All the files required to boot the system must be contained inside the root file system. Other file systems can be mounted as subdirectories within the root file system.
Every Linux prompt is actually a process that is associated with a location that maps to a directory within the file system. The directory a user is currently working in is known as the current working directory or present working directory (PWD). The name of this directory can be retrieved using the pwd command. It is possible to navigate from this point through the file system using either absolute or relative path names.
The root directory of most Linux file systems includes several standard, well-defined directories. The most important of these directories include the following:
How to List Files in Linux
The ls command provides a way to list files within a directory. However, many options allow for output filtering or to change what information to display.
The ls Command
ls
cities.txt countries.txt payroll states.txt states2.txt
ls wpbackup
public_html
ls -d */
accounts/ backup/ mysqlbackup/ phpcomposer/ wpbackup/
ls -l
-rw-rw-r-- 1 userid userid 29 Aug 31 14:51 cities.txt
-rw-rw-r-- 1 userid userid 42 Aug 31 14:51 countries.txt
drwxrwxr-x 2 userid userid 4096 Sep 2 16:51 payroll
-rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states.txt
-rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states2.txt
ls -la
drwxrwxr-x 3 userid userid 4096 Sep 3 10:56 .
drwxr-xr-x 10 userid userid 4096 Sep 1 08:48 ..
-rw-rw-r-- 1 userid userid 51 Sep 3 10:56 .states2.txt
-rw-rw-r-- 1 userid userid 29 Aug 31 14:51 cities.txt
-rw-rw-r-- 1 userid userid 42 Aug 31 14:51 countries.txt
drwxrwxr-x 2 userid userid 4096 Sep 2 16:51 payroll
-rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states.txt
-rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states2.txt
ls -lA
-rw-rw-r-- 1 userid userid 51 Sep 3 10:56 .states2.txt
-rw-rw-r-- 1 userid userid 29 Aug 31 14:51 cities.txt
-rw-rw-r-- 1 userid userid 42 Aug 31 14:51 countries.txt
drwxrwxr-x 2 userid userid 4096 Sep 2 16:51 payroll
-rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states.txt
-rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states2.txt
ls -lr
-rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states2.txt
-rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states.txt
drwxrwxr-x 2 userid userid 4096 Sep 2 16:51 payroll
-rw-rw-r-- 1 userid userid 42 Aug 31 14:51 countries.txt
-rw-rw-r-- 1 userid userid 29 Aug 31 14:51 cities.txt
ls -lS drwxrwxr-x 2 userid userid 4096 Sep 2 16:51 payroll -rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states.txt -rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states2.txt -rw-rw-r-- 1 userid userid 42 Aug 31 14:51 countries.txt -rw-rw-r-- 1 userid userid 29 Aug 31 14:51 cities.txt
ls -lt
drwxrwxr-x 3 userid userid 4096 Sep 2 16:51 accounts
drwxrwxr-x 2 userid userid 4096 Sep 1 08:47 backup
-rw-rw-r-- 1 userid userid 42 Aug 31 14:50 countries.txt
-rw-rw-r-- 1 userid userid 51 Aug 31 14:43 states.txt
drwxrwxr-x 3 userid userid 4096 Aug 24 17:08 phpcomposer
drwxrwxr-x 2 userid userid 4096 Jul 21 13:24 mysqlbackup
drwxrwxr-x 3 userid userid 4096 Jul 21 12:06 wpbackup
-rw-r--r-- 1 userid userid 196 Jul 20 14:42 testconnection2.php
ls -l | sort -k8
drwxrwxr-x 2 userid userid 4096 Sep 1 08:47 backup
drwxrwxr-x 3 userid userid 4096 Sep 3 10:56 accounts
drwxrwxr-x 3 userid userid 4096 Jul 21 12:06 wpbackup
drwxrwxr-x 2 userid userid 4096 Jul 21 13:24 mysqlbackup
-rw-r--r-- 1 userid userid 196 Jul 20 14:42 testconnection2.php
-rw-rw-r-- 1 userid userid 51 Aug 31 14:43 states.txt
-rw-rw-r-- 1 userid userid 42 Aug 31 14:50 countries.txt
drwxrwxr-x 3 userid userid 4096 Aug 24 17:08 phpcomposer
ls -lR
How to Change Directories in Linux
It is possible to navigate the Linux file system in a relative or absolute manner. An absolute path indicates the location of a directory from the root directory, and always begins with the / symbol. A relative path is defined in relation to the current working directory.
The cd command is used to change directories. This command is very straightforward to use, but it does have a few useful shortcuts. More information about the cd command can be found on the Linux man page.
The cd Command
pwd
/home/userid
cd accounts/payroll
cd ..
cd .
cd
领英推荐
cd /usr/bin
cd ~username
cd /usr/bin
cd ~/accounts
cd -
/usr/bin
How to Copy and Move Files in Linux
Files can be duplicated using the cp command. To move files, use the mv command.
The cp Command
cp states2.txt payroll
cp backup_file.sql backup_files.sql ~/accounts/payroll/
cp countries.txt countries_bkup.txt
cp -i states2.txt payroll cp -f states2.txt payroll
cp states2.txt payroll/states2_bkup.txt
cp -v countries.txt countries_bkup.txt
'countries.txt' -> 'countries_bkup.txt'
cp -r accounts/ backup
The mv Command
mv cities.txt payroll
mv -i cities.txt payroll
mv: overwrite 'payroll/cities.txt'?
mv cities.txt newcities.txt
How to Create Files and Directories in Linux
There are several methods to create files on a Linux system, but directories can only be created using the mkdir command.
The touch Command
An easy way to create a file is with the touch command. This method creates a new empty file in the current directory.
touch newfile1.txt
> newfile1.txt
touch newfile2.txt newfile3.txt
Note
The touch command can also be used to modify timestamps on existing files. The options list for this command is fairly extensive. See the Linux man page for more information.
The echo and cat Commands
There are two ways to create a file and immediately populate it with data. Both methods allow for the insertion of data directly from the command line. It is not necessary to open the file with a text editor or save the file.
echo "Placeholder for final text" > tmp_file.txt
cat > scores.txt
12-0
13-1
15-9
CTRL-D
cat scores.txt
12-0
13-1
15-9
The mkdir Command
The only way to create a new directory is with the mkdir command, but a few different options are available.
mkdir tmpdir
ls -l
drwxrwxr-x 2 userid userid 4096 Sep 6 11:50 tmpdir
mkdir tmpdir tmpdir2
mkdir /usr/bin/tmpdir
mkdir -m 777 tmpdir3
mkdir -p tmpdir/dir1/dir2
mkdir -p sports/{football,hockey,soccer,baseball,basketball}
cd sports
ls
baseball basketball football hockey soccer
How to Delete Files and Folders in Linux
Remove files using the rm command, and remove directories using either rm or rmdir. Consult the Linux man pages to learn more about the rm or rmdir commands.
Important
Recovering files that have been deleted with the rm command is somewhere between difficult and impossible. Take great care when using the rm or rmdir commands.
The rm and rmdir Commands
rm scores2.txt
rm scores.txt
rm: remove write-protected regular empty file 'scores.txt'? y
rm scores.txt scores2.txt
rm -i scores.txt rm -f scores.txt scores2.txt
rm *.txt
rmdir soccer rm -d hockey
rm -r sports