Directories
Navigating Directories
A directory contains a set of files. Directories can contain other directories, allowing files to be organized in a hierarchy.
Moving Around
At any time, you are inside of a directory. The pwd
command displays your current directory.
$ pwd
/home/myuser/code/kubernetes
cd
command.
$ cd /home/myuser/code/openshift
$ pwd
/home/myuser/code/openshift
..
shortcut.
$ pwd
/home/myuser/code/openshift
$ cd ..
$ pwd
/home/myuser/code
$ pwd
/home/myuser/code
$ cd kubernetes
$ pwd
/home/myuser/code/kubernetes
Listing Files in a Directory
The ls
command, by default, will list both files and directories in the current directory.
$ ls
existing-dir existing-file
-l
flag (which stands for "long") is commonly used to view more information about each file/directory. This extra information includes permissions, user and group ownership, file size, and timestamps.
$ ls -l
drwxr-xr-x. 2 myuser myuser 40 May 19 10:52 existing-dir
-rw-r--r--. 1 myuser myuser 12 May 19 10:52 existing-file
d
, the entry is a directory. Otherwise, it is a file.
-a
, used to indicate that all files (including the current directory and the parent) should be listed.
$ ls -a
. .. existing-dir existing-file
The above example introduced two shortcuts that can be used at the command line. The shortcut .
can be used to refer to the current directory. The other shortcut, ..
, refers to the parent of the current directory.
These two flags are frequently combined to give a reasonably detailed view of the current directory.
$ ls -al
total 4
drwxr-xr-x. 3 myuser myuser 80 May 19 10:52 .
drwxr-xr-x. 3 myuser myuser 60 May 19 10:45 ..
drwxr-xr-x. 2 myuser myuser 40 May 19 10:52 existing-dir
-rw-r--r--. 1 myuser myuser 12 May 19 10:52 existing-file
ls
command by itself, which defaults to the current directory. The full path to another directory can be specified to list its contents instead.
$ cd /
$ ls /home/myuser
total 4
drwxr-xr-x. 3 myuser myuser 80 May 19 10:52 .
drwxr-xr-x. 3 myuser myuser 60 May 19 10:45 ..
drwxr-xr-x. 2 myuser myuser 40 May 19 10:52 existing-dir
-rw-r--r--. 1 myuser myuser 12 May 19 10:52 existing-file
Creating & Deleting Directories
mkdir
command:
$ ls
existing-dir
$ mkdir my-new-dir
$ ls
existing-dir
my-new-dir
rm
command. Directories, however, require a special flag (-r
for "recursive") when being deleted.
Warning: This is, as you can imagine, a destructive action. Take care that the directory you specify is actually the one you intend to delete, as it cannot easily be recovered.
$ ls
existing-dir
my-new-dir
$ rm my-new-dir
rm: cannot remove 'my-new-dir/': Is a directory
$ rm -r my-new-dir
$ ls
existing-dir
Copying & Moving (& Renaming)
cp
command and specifying the file to be copied and its destination path and name.
$ ls
existing-dir existing-file
$ cp existing-file copy-file
$ ls
copy-file existing-dir existing-file
$ cp /home/myuser/existing-file /tmp/temp-copy
Just like when deleting directories, the -r
flag must be used when copying a directory.
$ cp existing-dir copy-dir
cp: -r not specified; omitting directory 'existing-dir'
$ cp -r existing-dir copy-dir
$ ls
copy-dir copy-file existing-dir existing-file
mv
command, specifying (in order) the file to be moved and the directory into which to move it.
$ ls
copy-dir copy-file existing-dir existing-file
$ mv copy-file /tmp
$ ls
copy-dir existing-dir existing-file
$ ls /tmp/copy-file
/tmp/copy-file
Unlike with cp
and rm
, the move command does not require a special flag when dealing with directories.
$ mv copy-dir /tmp
$ ls /tmp/copy-dir
/tmp/copy-dir
There is no specific "rename" command. Renaming a file or directory is done by simply moving (using the mv
command) the file to its new name.
$ ls
existing-dir existing-file
$ mv existing-file my-file
existing-dir my-file