Tạo file, thư mục trên Linux shell, Linux Essentials – LPIC
Playlist:
2024 02 18 05 53 48
Introduction
This lesson covers managing files and directories on Linux using command line tools.
A file is a collection of data with a name and set of attributes. If, for example, you were to transfer some photos from your phone to a computer and give them descriptive names, you would now have a bunch of image files on your computer. These files have attributes such as the time the file was last accessed or modified.
A directory is a special kind of file used to organize files. A good way to think of directories is like the file folders used to organize papers in a file cabinet. Unlike paper file folders, you can easily put directories inside of other directories.
The command line is the most effective way to manage files on a Linux system. The shell and command line tools have features that make using the command line faster and easier than a graphical file manager.
In this section you will use the commands ls, mv, cp, pwd, find, touch, rm, rmdir, echo, cat, and mkdir to manage and organize files and directories.
Case Sensitivity
Unlike Microsoft Windows, file and directory names on Linux systems are case sensitive. This means that the names /etc/ and /ETC/ are different directories. Try the following commands:
$ cd /
$ ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
$ cd ETC
bash: cd: ETC: No such file or directory
$ pwd
/
$ cd etc
$ pwd
/etc
The pwd shows you the directory you are currently in. As you can see, changing to /ETC did not work as there is no such directory. Changing into the directory /etc which exists, did succeed.
Creating Directories
The mkdir command is used to create directories.
Let’s create a new directory within our home directory:
$ cd ~
$ pwd
/home/user
$ ls
Desktop Documents Downloads
$ mkdir linux_essentials-2.4
$ ls
Desktop Documents Downloads linux_essentials-2.4
$ cd linux_essentials-2.4
$ pwd
/home/emma/linux_essentials-2.4
For the duration of this lesson, all commands will take place within this directory or in one of its subdirectories.
To easily return to the lesson directory from any other position in your file system, you can use the command:
$ cd ~/linux_essentials-2.4
The shell interprets the ~ character as your home directory.
When you’re in the lesson directory, create some more directories which we will use for the exercises. You can add all the directory names, separated by spaces, to mkdir:
$ mkdir creating moving copying/files copying/directories deleting/directories deleting/files globs
mkdir: cannot create directory ‘copying/files’: No such file or directory
mkdir: cannot create directory ‘copying/directories’: No such file or directory
mkdir: cannot create directory ‘deleting/directories’: No such file or directory
mkdir: cannot create directory ‘deleting/files’: No such file or directory
$ ls
creating globs moving
Notice the error message and that only moving, globs, and creating were created. The copying and deleting directories don’t exist yet. mkdir, by default, won’t create a directory inside of a directory that does not exist. The -p or –parents option instructs mkdir to create parent directories if they do not exist. Try the same mkdir command with the -p option:
$ mkdir -p creating moving copying/files copying/directories deleting/directories deleting/files globs
Now you don’t get any error messages. Let’s see which directories exist now:
$ find
[ad_2]
source