< Creating directories >
Creating a new, empty directory is very easy. You use the
mkdir
command:$
mkdir dir1That's it. It's really that easy!
< Removing directories >
There are two commands you can use for removing directories. If the directory is empty, you can use
rmdir
:$
rmdir dir1You can use
rmdir
only if the directory is empty. If you want to remove a directory with all its contents, you can use rm
with the -r
option. The -r
option tells rm
to remove a directory recursively:$
rm -r dir1It goes without saying that you can cause a lot of trouble with
rm -r
if you're not careful! In some cases it might be a good thing to use the -i
option when deleting a directory with its contents so that you'd be prompted before each file in the directory gets deleted:$
rm -ir dir1< Copying and moving directories >
For copying and moving directories you can use the
cp
and mv
commands just like you use them with files. If you've already tried to copy a directory with cp
, you've probably noticed that cp
just complains at you. Probably it says something like cp: omitting directory yadda yadda. You see, the cp
command wants you to use the -r
option if you want to copy a directory with its contents. The -r
means "copy recursively":$
cp -r dir1 dir2The above creates a directory named
dir2
whose contents will be identical to dir1
. However, if dir2
already exists, nothing will be overwritten: the directory dir1
will be copied into the dir2
directory under the name dir2/dir1
.When renaming directories, you use the
mv
command exactly the same way as with files:$
mv dir1 dir2When dealing with directories,
mv
works a bit like cp
does. If dir2
doesn't exist, the above will rename dir1
to dir2
, but if dir2
exists, the directory dir1
will be moved into the dir2
directory under the name dir2/dir1
. Moving around in the file system | |
Command | Action |
pwd | "Print working directory" - show what dir you're in. |
ls | List the contents of a dir. |
ls -l | List the contents of a dir and show additional info of the files. |
ls -a | List all files, including hidden files. |
cd | Change directory. |
cd .. | Go to the parent directory. |
Examining files | |
Command | Action |
file | Determine the type of a file. |
cat | Concatenate a file. |
less | View text files and paginate them if needed. |
Manipulating files and directories | |
Command | Action |
cp | Copy a file. |
cp -i | Copy a file and ask before overwriting. |
cp -r | Copy a directory with its contents. |
mv | Move or rename a file. |
mv -i | Move or rename a file and ask before overwriting. |
rm | Remove a file. |
rm -r | Remove a directory with its contents. |
rm -i | Ask before removing a file. Good to use with the -r option. |
mkdir | Make a directory. |
rmdir | Remove an empty directory. |
No comments:
Post a Comment