包阅导读总结
1.
关键词:Linux、File Management、Directories、Files、Commands
2.
总结:本文主要介绍了 Linux 系统中的文件管理,包括目录和文件的创建、删除、复制、移动等操作,还提及了导航目录结构、用户登录及权限等相关内容,并强调了命令的使用和注意事项。
3.
主要内容:
– Linux 文件管理概述
– 目录用于组织文件,操作系统也用其管理系统文件
– 用户创建和管理各种文件
– 目录结构导航
– 使用 `cd` 命令切换目录,`pwd` 查看当前位置
– 用户活动通常在 `/home` 目录下的用户主目录
– `~` 代表用户主目录
– 使用 `ls` 显示目录内容,需注意大小写敏感
– 创建和删除目录
– `mkdir` 创建目录,`rmdir` 删除空目录
– 创建和删除文件
– `touch` 创建空文件,`rm` 删除文件
– 复制和移动文件
– `cp` 复制文件,`mv` 移动和重命名文件
– 总结
– 文件管理是关键技能,需熟悉相关命令
思维导图:
文章地址:https://thenewstack.io/linux-file-management/
文章来源:thenewstack.io
作者:Damon M. Garn
发布时间:2024/7/11 17:12
语言:英文
总字数:2029字
预计阅读时间:9分钟
评分:84分
标签:Linux,文件管理,目录导航,Linux 命令,系统管理
以下为原文内容
本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com
Directories (also called folders) allow users to organize files. You would expect to find resumes and other job search materials in a directory namedresumes. The Linux OS also uses directories to manage operating system files.
Most users create and manage files that store various types of information. From sales reports to favorite movies, files contain the specific data we all need.
This tutorial looks at creating and deleting directories and files. It also covers commands like copy and move to help with file management.
You can work with the file management commands found in this section without any additional setup, but you may find it useful to review the Understand the Linux Command Line and the recent User and Group Management. Users must enter a name and password combination to log on to the system to create, delete, and maintain files, so understanding how to manage accounts is handy.
That article specifies three new users:fsmith,slee, andmgarcia. It also creates three groups:IT,HR, andPR. I’ll reference these throughout this article, so you may want to quickly create the users and groups using the useradd and groupadd commands.
Note: It is a poor security practice to log on to a Linux system as the root (administrator) user. Most systems force you to log on as a regular user and then use the sudo (super user do) command to elevate your privileges. You may be prompted for your password when usingsudo.
A follow-up article to this piece covers Linux permissions, which is how to control access to the files you create here for the users managed in the User and Group Management tutorial.
Refer to this tutorial if you’d like to build a lab environment to practice these commands.
Navigate the Directory Structure
You must know how to move or change from one directory to another. In a graphical user interface, you double-click on folders to browse into them. In a command line environment, you issue commands to navigate from directory to directory. Thecd (change directory) command moves you from one folder to another.
For example, to move to a directory named/etc, type:
You can check your location in the folder structure at any time by using thepwd command:

Figure 1: Using the cd and pwd commands.
Take a few minutes to navigate through some directories yourself. Use thecd /media andcd /bootcommands to practice navigating. Enter thepwd command to check your location.
Most user activities take place in the user’s account’s home folder, which is found inside the/home directory. Suppose you’re logged in as a user namedfsmith. Your home directory is probably at/home/fsmith. If you’re not sure which account you’re logged in as, type the whoami command.

Figure 2: Using the whoami and pwd commands.
After you practice navigating through some directories usingcd andpwd, typecd /home/fsmith (or whatever user name you have).
Linux includes many features to make your life easier. One is using the~ character (look in the upper left corner of your keyboard) to represent the path to your home folder. For example, if your home directory is/home/fsmith, you could just typecd ~ to get there (instead of typing the whole path).
Type the ls command to display the contents of the directory. This shows you any directories or files inside the current directory. You may see folders likeDocuments,Downloads,Music, and others on your system.
Be careful of Linux’s case sensitivity. Directory names, file names, commands, and even options are all case-sensitive. You must get these values correct.
Commands covered in this section:
Create and Delete Directories
There are two commands for managing directories: mkdir and rmdir. Themkdircommand creates a new directory and thermdir command removes existing directories.
Create Directories
Typecd ~ to confirm you’re in your home directory. Create some directories to organize business documents for a mock organization. Begin by using themkdir command to create a directory nameddepartments.

Figure 3: Using mkdir to create the departments directory.
If you use thels command, you should see thedepartments folder listed as a subdirectory of your home directory. The idea is to simulate department-specific directories for a mock company.
Change to thedepartments folder using thecd command. Use thepwd command to confirm the change.

Figure 4: Changing to the departments directory.
Using the same command, create subdirectories in thedepartments folder namedhr_dept,pr_dept, andit_dept. Confirm they exist by using thels command.

Figure 5: Creating three subdirectories in the departments directory.
Delete Directories
Deleting directories is equally straightforward. Note that to use thermdir command, the directory must be empty of files and subdirectories.
Create a new folder calledtest in your home directory and usels to verify it exists.
Next, type the following command to delete an empty directory namedtest:
$ rmdir test
Note that some Linux distributions prompt for confirmation before deleting and some do not.
Commands covered in this section:
Create and Delete Files
Directories are a way to organize files. The next logical step is to create some files. Don’t concern yourself with writing any real information in the files right now. This article focuses on files as objects and how administrators manipulate them.
Create Files
The touch command creates an empty file in a specified location. It’s a great way to quickly create a file to work with during exercises like this one.
Return to your home directory for this step using thecd .. command (that’scd, a space, then two dots).
Thecd .. command moves you one directory higher in the file structure. Yourpwd should be/home/fsmith.
Use thetouch command to create an object namedfileA.txt in your home directory (not one of the department directories created earlier).
Use thels command to verify thatfileA.txt exists in your home directory.

Figure 6: Using touch to create a file.
Remember that Linux is case sensitive, therefore,fileA.txt is different fromfilea.txt.
Use thecd command to change to thedepartments directory you created earlier. Change to theit_dept directory next. Thepwd command should show/home/fsmith/departments/it_dept.
Use thetouch command to create a file namedpassword–reset.txt in theit_dept directory.
$ touch password–reset.txt |
You’ve now created some IT documentation in theit_dept directory! Feel free to create other files, such aslaptop–inventory.txt orlearning–resources.txt.

Figure 7: Using touch to create files.
Typecd .. to move up one directory in the structure. Yourpwd should be/home/fsmith/departments. Change to thehr_dept directory and create a file namedpolicies.txt. Using the same steps, change to thepr_dept directory and create a file namedpress–releases.txt.

Figure 8: Adding files to the various department directories.
Feel free to create additional files to practice working with thetouch command.
Delete Files
Thetouch command creates files, but what about deleting files? Use the rm command to delete files from the system.
Change to your home directory by typingcd ~ and create a file nameddelete–me by using thetouch command.
Delete thedelete–me file using therm command:
Carefully practice usingrm. Note that by default there is no “trash can” at the Linux command line. Anything you delete is actually gone and generally unrecoverable. As withrmdir, some Linux distributions prompt you to confirm you want to delete the file and others do not.
Commands covered in this section:
Copy and Move Files
Creating and deleting files is certainly useful, but you will probably want to copy and move them, too. Use the following examples to practice the copy and move commands.
I’ll begin with a simple syntax rule to help you use both commands. The syntax of the copy and move function is “from here to there.” You’ll designate the source or original file first (the “from here” part), then the destination to copy or move the file to (the “to there” part).
Imagine using a sentence to conduct the action: “Move fileA.txt from here in the test directory to over there in the extras directory.”
The next two sections provide details.
Copy Files
The command to copy files from one location to another is cp. When integrated with the syntax explanation above, the command readscp (from here) (to there).
Create two directories in your home folder namedtest andextras. CopyfileA.txt fromtest toextras (create thefileA.txt file usingtouchif it doesn’t already exist). Type:
The source or “from” part isfileA.txt and the destination to copy the file to isextras. You should now have a duplicate offileA.txt in thehome andextrasdirectories. Verify this with thels command.

Figure 9: Copying fileA.txt to the extras directory.
Practice thecp command with some test files.
Move (and Rename) Files
The mv (move) command doesn’t create a file duplicate likecp does. Otherwise, the syntax is the same — move from here to there.
Usetouch to createfileB.txt and then movefileB.txt toextras by typing:

Figure 10: Moving a file to the extras directory.
Pay attention to the difference between copy (which creates a duplicate of the file in a specified location) and move (which actually moves the file to a specified location).
Interestingly, Linux doesn’t really have a specific “rename” command. Instead, you move the file from the current location to the current location with a new name. Confusing? It’s actually simple.
To renamefileA.txt tofileZ.txt, type:

Figure 11: Using the mv command to rename a file.
Use thels command to confirm the rename function worked. Create and rename a few files for practice.
Commands covered in this section:
Wrap Up
File management is a critical daily skill for Linux users. You’ll use directories to organize similar types of resources, such as music files or business documents. Your resources will be files you write using text editors, files you’ve downloaded, or maybe even programs you’ve written. Knowing how to create, move, copy, and delete files is essential.
Practice these skills until they become second nature. Don’t forget to usecd andpwdto navigate from directory to directory. Your next step should be learning how to use standard Linux permissions to control access to files and directories for specific user accounts and groups.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don’t miss an episode. Subscribe to our YouTubechannel to stream all our podcasts, interviews, demos, and more.