Common Linux Command-Line Tools

Basic Linux Commands

Welcome to this general tutorial on basic Linux commands. To get started you
will need a Linux Distribution. Thats it. All Distributions come with a shell
enviornment, whether its a desktop-interface-shell like mate-terminal (like i
use) to an initial command line if you dont have a desktop manager. This is a
very common method of getting around your file system and doing routine
maintenance on your Linux Distribution. I am currently using Debian 10, with
mate desktop enviornment installed , and i use the mate-terminal which i believe
sets me up with a BASH enviornment . I would also like to mention if you go to
google and type :
man program_name
you will get a detailed manual of the program with command line flags and
usable variables.

1. pwd:

pwd stands for print working directory. this will tell you where you are in
your file system. There is a couple flags you can type in to manipulate this, for example the
“-L” or “–logical” flag uses PWD from the enviornment, even if it contains
symlinks. If you use “-P” or “–physical” it will avoid all symlinks. You can
also type “–help” to display the options.

[sourcecode language=’bash’]
clim@debian:~/Desktop/Tests$ pwd –help
pwd: pwd [-LP]
Print the name of the current working directory.

Options:
-L print the value of $PWD if it names the current working
directory
-P print the physical directory, without any symbolic links

By default, `pwd’ behaves as if `-L’ were specified.

Exit Status:
Returns 0 unless an invalid option is given or the current directory
cannot be read.
clim@debian:~/Desktop/Tests$ pwd
/home/clim/Desktop/Tests
clim@debian:~/Desktop/Tests$
[/sourcecode]

2. ls:
ls is a command which will list the files in your current directory and sorts
them alphabetically or by other attributes based on commandline flags. This will
also list hidden files in your current directory if you give the -a flag. If you
want more information about the ls command, you can type “ls –help” to get all
the flags you can use.

[sourcecode language=’bash’]
clim@debian:~$ ls
bin diff.txt info.err nano.save Pictures src
Cpp Documents Libre-OS obj Public Templates
Desktop Downloads Music opt remove.txt Videos
clim@debian:~$ ls -a
. Downloads .nuget .themes
.. .emacs.d obj .var
.atom .gconf opt .vboxclient-clipboard.pid
.bash_history .gnome2 Pictures .vboxclient-display.pid
.bash_logout .gnupg .pki .vboxclient-draganddrop.pid
.bashrc .ICEauthority .profile .vboxclient-seamless.pid
bin .icons Public Videos
.cache info.err .recently-used .viminfo
.config Libre-OS remove.txt .Xauthority
Cpp .local src .xsession-errors
Desktop .mozilla .ssh .xsession-errors.old
diff.txt Music .swp
.dmrc .nano .templateengine
Documents nano.save Templates
clim@debian:~$
[/sourcecode]

3. cd

cd stands for Change Directories. This will move you to to the directory you
specify. This has similar flags to the pwd command. You can supply it “-L” to
force symbolic links to be followed. This is the default flag that gets used if
no other flag is specified. The Next flag is “-P” which use the physical
directory structure without following symbolic links. The “-e” is given if the
“-P” flag is specified. If the current working directory cannot be determined
(For example, you tried to change to a symbolic link even tho you specified
“-P”) then the exit status will return a non-zero number. And last, on systems
that support it, “-@” will create a file with extended attributes as a directory
containing the file attributes. All of this information is provided by
“cd –help”.

[sourcecode language=’bash’]
clim@debian:~$ ls
bin diff.txt info.err nano.save Pictures src
Cpp Documents Libre-OS obj Public Templates
Desktop Downloads Music opt remove.txt Videos
clim@debian:~$ cd Documents/
clim@debian:~/Documents$ ls
Project
clim@debian:~/Documents$ cd Project/
clim@debian:~/Documents/Project$ ls
ServerProgram
clim@debian:~/Documents/Project$ cd ../../Public
clim@debian:~/Public$ ls
clim@debian:~/Public$ cd ../
clim@debian:~$

[/sourcecode]

4. touch

touch will update the access and modification times of each specified file to
the current time. This is useful if you want to quickly create a file. The
following is supplied using “touch –help”

[sourcecode language=’bash’]
clim@debian:~$ touch –help
Usage: touch [OPTION]… FILE…
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of – is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, –no-create do not create any files
-d, –date=STRING parse STRING and use it instead of current time
-f (ignored)
-h, –no-dereference affect each symbolic link instead of any referenced
file (useful only on systems that can change the
timestamps of a symlink)
-m change only the modification time
-r, –reference=FILE use this file’s times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
–time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m
–help display this help and exit
–version output version information and exit

Note that the -d and -t options accept different time-date formats.

GNU coreutils online help:
Full documentation at:
or available locally via: info ‘(coreutils) touch invocation’
clim@debian:~$
[/sourcecode]

[sourcecode language=’bash’]
clim@debian:~$ ls
bin diff.txt info.err nano.save Pictures src
Cpp Documents Libre-OS obj Public Templates
Desktop Downloads Music opt remove.txt Videos
clim@debian:~$ touch testTouch
clim@debian:~$ ls
bin diff.txt info.err nano.save Pictures src Videos
Cpp Documents Libre-OS obj Public Templates
Desktop Downloads Music opt remove.txt testTouch
clim@debian:~$
[/sourcecode]

5. cat
This program concatenates files to standard output . This is useful for when
you are checking the contents of a file.

[sourcecode language=’bash’]
clim@debian:~$ ls
bin diff.txt info.err nano.save Pictures src testTouch
Cpp Documents Libre-OS obj Public Templates Videos
Desktop Downloads Music opt remove.txt testCat
clim@debian:~$ cat testCat
Hello from testCat
clim@debian:~$
[/sourcecode]

Here is the help page of cat:
[sourcecode language=’bash’]
clim@debian:~$ cat –help
Usage: cat [OPTION]… [FILE]…
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

-A, –show-all equivalent to -vET
-b, –number-nonblank number nonempty output lines, overrides -n
-e equivalent to -vE
-E, –show-ends display $ at end of each line
-n, –number number all output lines
-s, –squeeze-blank suppress repeated empty output lines
-t equivalent to -vT
-T, –show-tabs display TAB characters as ^I
-u (ignored)
-v, –show-nonprinting use ^ and M- notation, except for LFD and TAB
–help display this help and exit
–version output version information and exit

Examples:
cat f – g Output f’s contents, then standard input, then g’s contents.
cat Copy standard input to standard output.

GNU coreutils online help:
Full documentation at:
or available locally via: info ‘(coreutils) cat invocation’
clim@debian:~$
[/sourcecode]

6. mkdir
mkdir is used to make directories. Whenever i setup a new programming project
this is the first command i use, to keep my project isolated. It has multiple
command-line arguments you can pass in. Here is the help argument:

[sourcecode language=’bash’]
clim@debian:~$ mkdir –help
Usage: mkdir [OPTION]… DIRECTORY…
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
-m, –mode=MODE set file mode (as in chmod), not a=rwx – umask
-p, –parents no error if existing, make parent directories as needed
-v, –verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
–context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
–help display this help and exit
–version output version information and exit

GNU coreutils online help:
Full documentation at:
or available locally via: info ‘(coreutils) mkdir invocation’
clim@debian:~$ ^C
[/sourcecode]

For the next few, i will let the help prompts speak for them:

7. rmdir

[sourcecode language=’bash’]
clim@debian:~$ rmdir –help
Usage: rmdir [OPTION]… DIRECTORY…
Remove the DIRECTORY(ies), if they are empty.

–ignore-fail-on-non-empty
ignore each failure that is solely because a directory
is non-empty
-p, –parents remove DIRECTORY and its ancestors; e.g., ‘rmdir -p a/b/c’ is
similar to ‘rmdir a/b/c a/b a’
-v, –verbose output a diagnostic for every directory processed
–help display this help and exit
–version output version information and exit

GNU coreutils online help:
Full documentation at:
or available locally via: info ‘(coreutils) rmdir invocation’
clim@debian:~$
[/sourcecode]

8. cp
This command is used specifically to copy files.

[sourcecode language=’bash’]
clim@debian:~$ cp –help
Usage: cp [OPTION]… [-T] SOURCE DEST
or: cp [OPTION]… SOURCE… DIRECTORY
or: cp [OPTION]… -t DIRECTORY SOURCE…
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
-a, –archive same as -dR –preserve=all
–attributes-only don’t copy the file data, just the attributes
–backup[=CONTROL] make a backup of each existing destination file
-b like –backup but does not accept an argument
–copy-contents copy contents of special files when recursive
-d same as –no-dereference –preserve=links
-f, –force if an existing destination file cannot be
opened, remove it and try again (this option
is ignored when the -n option is also used)
-i, –interactive prompt before overwrite (overrides a previous -n
option)
-H follow command-line symbolic links in SOURCE
-l, –link hard link files instead of copying
-L, –dereference always follow symbolic links in SOURCE
-n, –no-clobber do not overwrite an existing file (overrides
a previous -i option)
-P, –no-dereference never follow symbolic links in SOURCE
-p same as –preserve=mode,ownership,timestamps
–preserve[=ATTR_LIST] preserve the specified attributes (default:
mode,ownership,timestamps), if possible
additional attributes: context, links, xattr,
all
–no-preserve=ATTR_LIST don’t preserve the specified attributes
–parents use full source file name under DIRECTORY
-R, -r, –recursive copy directories recursively
–reflink[=WHEN] control clone/CoW copies. See below
–remove-destination remove each existing destination file before
attempting to open it (contrast with –force)
–sparse=WHEN control creation of sparse files. See below
–strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-s, –symbolic-link make symbolic links instead of copying
-S, –suffix=SUFFIX override the usual backup suffix
-t, –target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY
-T, –no-target-directory treat DEST as a normal file
-u, –update copy only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, –verbose explain what is being done
-x, –one-file-system stay on this file system
-Z set SELinux security context of destination
file to default type
–context[=CTX] like -Z, or if CTX is specified then set the
SELinux or SMACK security context to CTX
–help display this help and exit
–version output version information and exit

By default, sparse SOURCE files are detected by a crude heuristic and the
corresponding DEST file is made sparse as well. That is the behavior
selected by –sparse=auto. Specify –sparse=always to create a sparse DEST
file whenever the SOURCE file contains a long enough sequence of zero bytes.
Use –sparse=never to inhibit creation of sparse files.

When –reflink[=always] is specified, perform a lightweight copy, where the
data blocks are copied only when modified. If this is not possible the copy
fails, or if –reflink=auto is specified, fall back to a standard copy.
Use –reflink=never to ensure a standard copy is performed.

The backup suffix is ‘~’, unless set with –suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the –backup option or through
the VERSION_CONTROL environment variable. Here are the values:

none, off never make backups (even if –backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups

As a special case, cp makes a backup of SOURCE when the force and backup
options are given and SOURCE and DEST are the same name for an existing,
regular file.

GNU coreutils online help:
Full documentation at:
or available locally via: info ‘(coreutils) cp invocation’
clim@debian:~$
[/sourcecode]

9. rm
If i need to remove a file, i can simply use the rm command.

[sourcecode language=’bash’]
clim@debian:~$ rm –help
Usage: rm [OPTION]… [FILE]…
Remove (unlink) the FILE(s).

-f, –force ignore nonexistent files and arguments, never prompt
-i prompt before every removal
-I prompt once before removing more than three files, or
when removing recursively; less intrusive than -i,
while still giving protection against most mistakes
–interactive[=WHEN] prompt according to WHEN: never, once (-I), or
always (-i); without WHEN, prompt always
–one-file-system when removing a hierarchy recursively, skip any
directory that is on a file system different from
that of the corresponding command line argument
–no-preserve-root do not treat ‘/’ specially
–preserve-root[=all] do not remove ‘/’ (default);
with ‘all’, reject any command line argument
on a separate device from its parent
-r, -R, –recursive remove directories and their contents recursively
-d, –dir remove empty directories
-v, –verbose explain what is being done
–help display this help and exit
–version output version information and exit

By default, rm does not remove directories. Use the –recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.

To remove a file whose name starts with a ‘-‘, for example ‘-foo’,
use one of these commands:
rm — -foo

rm ./-foo

Note that if you use rm to remove a file, it might be possible to recover
some of its contents, given sufficient expertise and/or time. For greater
assurance that the contents are truly unrecoverable, consider using shred.

GNU coreutils online help:
Full documentation at:
or available locally via: info ‘(coreutils) rm invocation’
clim@debian:~$
[/sourcecode]

10. mv
Unlike the cp command which copies files, the mv will move the files completely
to the destination.

[sourcecode language=’bash’]
clim@debian:~$ mv –help
Usage: mv [OPTION]… [-T] SOURCE DEST
or: mv [OPTION]… SOURCE… DIRECTORY
or: mv [OPTION]… -t DIRECTORY SOURCE…
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
–backup[=CONTROL] make a backup of each existing destination file
-b like –backup but does not accept an argument
-f, –force do not prompt before overwriting
-i, –interactive prompt before overwrite
-n, –no-clobber do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
–strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-S, –suffix=SUFFIX override the usual backup suffix
-t, –target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
-T, –no-target-directory treat DEST as a normal file
-u, –update move only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, –verbose explain what is being done
-Z, –context set SELinux security context of destination
file to default type
–help display this help and exit
–version output version information and exit

The backup suffix is ‘~’, unless set with –suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the –backup option or through
the VERSION_CONTROL environment variable. Here are the values:

none, off never make backups (even if –backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups

GNU coreutils online help:
Full documentation at:
or available locally via: info ‘(coreutils) mv invocation’
[/sourcecode]

Leave a Reply