Basic file operation using the command line#

Changing the working directory#

The shell and actually any Linux process has a (current) working directory. File operations without absolute paths will always be interpreted with respect to this working directory. It is important to keep track where “the shell is located”. As a hint the last level of the current path is shown in the command prompt. The pwd command (print working directory), well, prints the working directory and the cd command (change directory) takes a path as argument and changes the working directory accordingly.

[john@laptop ~]$ pwd
/home/john

# change the working using an absolute path
[john@laptop ~]$ cd /home/john/foo     
[john@laptop foo]$ pwd
/home/john/foo

# change the working directory using a relative path
[john@laptop foo]$ cd bar              
[john@laptop bar]$ pwd
/home/john/foo/bar

# change the working directory the users home directory
[john@laptop bar]$ cd                  
[john@laptop ~]$ pwd
/home/john

Note

A common pattern is go up the directory tree by one level with cd .. and using .. as name for the parent directory.

Inspecting directory contents#

The ls command lists files in a directory and quick overview about the directory hierarchy itself is shown by tree. The output of both commands can be controlled with various parameters.

[john@laptop ~]$ ls foo
bar  baz  filaA  fileB

[john@laptop ~]$ tree foo
foo
├── bar
│   └── fileC
├── baz
│   └── fileD
├── fileA
└── fileB

Creating and deleting directories#

There are two commands to “make” and “remove” directories mkdir and rmdir. They take a path as argument and are straight forward to use.

[john@laptop ~]$ mkdir new_dir
[john@laptop ~]$ rmdir new_dir

Note

rmdir will refuse to remove non-empty directories. This is a safety measure, all the contained files and directories have to be removed upfront. Files are removed with rm which also removes directories, so rmdir is seldom used.

Moving and renaming files#

The mv command renames or moves files. The order of arguments is, like always, mv source target. As long as no file system boundaries are crossed a move or renaming action just manipulates the related dentry objects. Inodes and file data are usually not touched. In particular no data is copied and the operation is very fast.

If the target is an existing directory mv will keep source’s name and move the file into the target directory. In that case it is also possible to give several source names to move multiple files at once. This can be conveniently combined with bash’s path name expansion.

[john@laptop foo]$ mv fileA fileB bar
[john@laptop foo]$ tree
.
├── bar
│   ├── fileA
│   ├── fileB
│   └── fileC
└── baz
    └── fileD

Note

If the target is an existing file mv will overwrite that file! To be save there -i enables the interactive mode, which will ask for confirmation before overwriting files

[john@laptop foo]$ mv -i baz/filaA bar/fileC
mv: overwrite 'bar/fileC'? n

manipulate files#

mv SOURCE TARGET cp SOURCE TARGET cp -r SOURCE TARGET rm FILE scp SRC host:TARGET scp host:SRC TARGET mkdir TRAGET rsync tree –du -h