␡
- Starting Up the Terminal
- Getting Started
- Building Pipelines
- Running Commands as Superuser
- Finding Help
- Moving around the Filesystem
- Manipulating Files and Folders
- System Information Commands
- Searching and Editing Text Files
- Dealing with Users and Groups
- Getting Help on the Command Line
- Searching for Man Files
- Using Wildcards
- Executing Multiple Commands
- Moving to More Advanced Uses of the Command Line
This chapter is from the book
Manipulating Files and Folders
You can manipulate files and folders with the following commands.
-
cp
: Thecp
command makes a copy of a file for you. For example,cp
filefoo
makes an exact copy of the file whose name you entered and names the copy foo, but the first file will still exist with its original name. -
mv
: Themv
command moves a file to a different location or renames a file. Examples are as follows:mv file foo
renames the original file to foo.mv foo ~/Desktop
moves the file foo to your desktop directory but does not rename it. You must specify a new filename to rename a file. After you usemv
, the original file no longer exists, but after you usecp
, as above, that file stays and a new copy is made. - To save on typing, you can substitute
~
in place of the home directory, so/home/username/pictures
is the same as~/pictures.
-
rm
: Use this command to remove or delete a file in your directory, as inrm file.txt
. It does not work on directories that contain files, which must first be emptied and may then be deleted using thermdir
command. There are some advanced cases where you may userm
to remove directories, but discussing those are beyond the intent of this chapter. -
ls:
Thels
command shows you the files in your current directory. Used with certain options, it lets you see file sizes, when files where created, and file permissions. For example,ls ~
shows you the files that are in your home directory. -
mkdir
: Themkdir
command allows you to create directories. For example,mkdir music
creates a music directory. -
chmod
: Thechmod
command changes the permissions on the files listed. Permissions are based on a fairly simple model. You can set permissions for user, group, and world, and you can set whether each can read, write, and/or execute the file. For example, if a file had permission to allow everybody to read but only the user could write, the permissions would readrwxr—r—
. To add or remove a permission, you append a+
or a-
in front of the specific permission. For example, to add the capability for the group to edit in the previous example, you could typechmod g+w file
. -
chown
: Thechown
command allows the user to change the user and group ownerships of a file. For example,sudo chown jim file
changes the ownership of the file to Jim.