managing files
Working W/Wildcards
When working with files, using wildcards can make your work a lot easier. A wildcard is a shell feature that helps you referring to multiple files in an easy way.
Wildcard Overview
Wildcard | Use |
---|---|
* |
Refers to an unlimited number of all characters |
? |
Used to refer to one specific character that can be any character. ls c?t would match cat as well as cut |
[auo] |
Refers to one character that may be selected from the range that is specified between square brackets. ls c[auo]t would match cat, cut, and cot. |
Working W/Absolute and Relative Pathnames
An absolute filename, or absolute path, is a complete path reference to the file or directory you want to work with. This pathname starts with the root directory, followed by all subdirectories up to the actual filename. No matter what your current directory is, absolute filenames will always work. /var/log/httpd/error_log
A relative filename is relative to the current directory as shown with the pwd
command. It contains only the elements that are required to get from the current directory up to the item you need.
Listing Files and Directories
Command | Use |
---|---|
ls -l |
Shows a long listing, which includes information about file properties, such as creation date and permissions. |
ls -a |
Shows all files, including hidden files. |
ls -ltra |
Shows all files, including hidded files sorted on modification date. Most recently modified files last in the list. |
ls -d |
Shows the names of directories, not the contents of all directories that match the wildcards that have been used with the ls command. |
ls -R |
Shows the names of directories, not the contents of all directories that match the wildcards that have been used with the ls command. |
Copying Files
Copy files using the cp
command. Just use cp /path/to/file /path/to/destination
. To copy the file /etc/hosts
to the /tmp
directory for instance, use cp /etc/hosts /tmp
.
With the cp
command, you can also copy an entire subdirectory, with its contents and everything within it. Use the -R
option, which stands for recursive. To copy the directory /etc
and everything in it to /tmp
, you would, for instance, use the command cp -R /etc /tmp
. If you want to make sure that you keep the current permissions, use the -a
option, which has cp work in archive mode.
A special case when working with cp
are hidden files. By default, hidden files are not copied over. There are three solutions to work with hidden files:
cp /directory/.* /tmp
This copies all files that have a name string with a dot.
to/tmp
cp -a /directory/ .
This copies the entire directory/directory
, including its contents, to the current directory.cp -a /directory/. .
This copies all files, regular and hidden, to the current directory.
Moving Files
To move files, you use the mv
command. This command removes the file from its current location and puts it in the new location. You can also use mv
to rename a file.
mv file1 /tmp
Moves file1 from the current directory to/tmp
mkdir directory1; mv directory1 /tmp
Creates directory1 and moves it to/tmp
mv file1 file1-NEW
Renames file1 to file1-NEW
Deleting Files
To delete files and directories, you use the rm
command. When used on a single file, the single file is removed. You can also use it on directories that contain files, use the -R
option, which stands for recursive.