Basic Commands
Objective
- Get familiar with the file system structure.
- Perform essential commands.
Summary
cat
cp
date
file
head
less
ls
mkdir
more
most
mv
rmdir
rm
tail
touch
whoami
who
Filenames
- Every directory/file has a name.
- Filenames are case sensitive.
- Filenames cannot include the slash
/
nor theNUL
(\0
) characters. - Maximum filename length is 255 bytes.
- File extensions are not required but may be needed by certain applications.
- Files that start with
.
are called hidden files and are not normally displayed.
Listing Files
ls
Syntax
ls [-option] [file or directory name]
- Used to list the contents of a directory.
- At its most basic form,
ls
lists the filenames only.
Example
> ls /etc
abrt grub2.cfg prelink.conf.d
adjtime grub.d printcap
aliases gshadow profile
aliases.db gshadow~ profile.d
...
ls
accepts options which can change the way the files are listed, including:
Option | Action |
---|---|
-a | list all files including system files |
-F | show file type (* after executables, / after directories, nothing after plain files) |
-l | show extended file information (file size, permissions and owner information) |
-r | list file in reverse order |
Identifying File Types
No rules are enforced when it comes to file contents and filename extensions, making it difficult to identify the file type from the filename.
file
file
is used to look inside a file and identify its type.
Syntax
file filename
- Some common file types:
- empty
- directory
- ASCII text
- Bourne-Again shell script
- C source
- PDF document
- JSON document data
- gzip compressed data
Example
> file /etc
/etc: directory
> file /bin/cat
/bin/cat: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV),
dynamically linked, interpreter /lib/ld-linux-aarch64.so.1,
BuildID[sha1]=0ced1703d50f352986337e10825110c509a35677, for GNU/Linux 3.7.0, stripped
Viewing Files
There are two basic commands used to display the contents of an ASCII text file: cat
and more
.
cat
- Displays the contents of a file all at once.
- Useful for small files.
- Short for concatenate.
- Can be used to join files together.
- No flow control (must use
^S
and^Q
to pause and restart the output).
Syntax
cat file [file ...]
Example
> cat /etc/bashrc
# /etc/bashrc
# System wide functions and ...
more
- Displays a file one screen at a time.
- Useful for viewing large files.
- More flexible than
cat
.
Syntax
more file [file ...]
Example
> more /etc/bashrc
# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
...
--More--(37%)
-
Prompt usually indicates the percentage of the file that has been viewed.
-
Some built-in commands that are similar to
vim
commands:Name Function <space>
display next screen <return>
display next line b
display previous screen (back) q
orQ
or<interrup>
exit from more
=
Display current line number /pattern
search for next occurrence of pattern n
search for next occurrence of previously searched pattern
!<cmd>
execute <cmd>
in a subshellv
start up /usr/bin/vi
at current lineh
help, display this message :f
display current filename and line number
There are other utilities that enhance on more
, namely less
and most
.
Exercise 1
- At the command prompt, enter:
ls
. This lists the files in your current directory. - Enter
ls --help
to display available options for thels
command - To display the contents of the
hello.cpp
file, enter:cat hello.cpp
- Display the contents of the
/etc/passwd
file. - Enter the following command to view the
/etc/passwd
file a page at a time:more /etc/passwd
- Hit
<return>
to display the next line. - Hit the
<space>
to proceed to the next screen. - Press
q
to quit and return to the prompt. - Read the manual page for the
more
command by entering:man more
Themore
utility is used to paginate the manual pages. Use the<space>
and<return>
key to move through the pages. - Identify the option used to display command help while in
more
. - Quit from the manual page and return to the prompt.
- Enter the following commands and identify the differences:
ls /bin/cat
ls -F /bin/cat
ls -l /bin/cat
Listing Active Users
who
- Shows all active users/sessions on the system.
Syntax
who [-uHb]
- Displays username, terminal, login time, and other information.
- option
-u
adds idle time. whoami
displays your login information.
Example
> who
> who -u
> whoami
> who -H
> who -b
Exercise 2
- See who is logged in to the system by typing:
who
. - Show who you are logged in as using:
whoami
. - Check the man pages and explain what does
who -b
do?
Reading the Date / Time
date
- Displays the system date and time.
Syntax
date [+format_string]
Example
> date
Sat Sep 3 11:46:25 AM +03 2023
> date +"Year: %Y, Month: %m, Day: %d"
Year: 2023, Month: 09, Day: 03
Creating Files
touch
- Used to create empty files or update the time stamp on existing files.
Syntax
touch filename
Example
> ls .lock
ls : .lock: No such file or directory
> touch .lock
> touch .lock
> ls -l .lock .bashrc
-rw-r--r--. 1 user group 522 Jul 19 2023 .bashrc
-rw-r--r--. 1 user group 0 Jan 28 17:25 .lock
> touch .bashrc
Expansion using Wildcards
The shell interprets special characters as wildcards which represent characters in filenames:
Character | Meaning | Example |
---|---|---|
? | any single character | file? ????? |
* | 0 or more characters | a* *.doc |
[...] | range or class of characters ( ! negates range or class) | [abc]* [a-z]*[0-9] *[!A-Za-z0-9] |
{start..end} | build a sequence of characters | {10..0} {13..24} {b..g} |
Example
> ls
> touch memo{a..z}
> ls memo*
> ls ?.c
> ls ?????
> touch part{00..99}
> ls part[2346]
> ls part[1-46]
> ls *[1-46]
> echo {0..10}
Exericse 3
-
Show the system date and time by typing:
date
. -
Use
touch
to create the following zero-length files in your directory:file1 file2 file3 file4 file5 file3a files chapter1 Friends filelist Foes
touch
accepts multiple filenames as arguments. -
Use
ls
to list the files in your directory. -
Use the
*
,?
, and[]
wildcard characters as shown below to list specific files only:ls file*
ls file[1-3]
ls [A-Z]*
ls file?
ls *
Creating Directories
mkdir
- Directories help you organize your files into logical groups.
Syntax
mkdir dirname
Example
> pwd
/home/tux
> ls -F
> mkdir test_dir
> ls -F
test_dir/
> cd test_dir/
> pwd
/home/tux/test_dir
> ls -aF
./ ../
> cd ..
> pwd
/home/tux
> mkdir /etc/junk
mkdir: cannot create directory `/etc/junk': Permission denied
> mkdir /tmp/junk
> mkdir dir_a dir_b dir_c
> ls -F
dir_a/ dir_b/ dir_c/ test_dir/
Removing Directories
rmdir
- Used to delete empty directories.
Syntax
rmdir dirname
- Cannot be your current directory (
.
) or parent of your current directory (..
). - Must delete the directory contents prior to using
rmdir
.
Example
> pwd
/home/tux
> ls -F
dir_a/ dir_b/ dir_c/ test_dir/
> touch dir_a/file_a
> touch dir_c/veryimportantfile
> rmdir dir_b
> rmdir dir_c
rmdir: 'dir_c': Directory not empty
> rmdir .
rmdir: failed to remove '.': Invalid argument
rm
- Used to recursively remove non-necessarily empty directory structures with the
-r
option.
Syntax
rm -r dirname
- Can have catastrophic consequences if used improperly!
- As a precaution, use it with the
-i
option as well.
Example
> pwd
/home/tux
> ls -F
dir_a/ dir_c/ test_dir/
> ls dir_a
file_a
> rm -r dir_a
> ls -F
dir_c/ test_dir/
> ls dir_c
veryimportantfile
> rm -r dir_c
> ls
test_dir
Exercise 4
Enter the following commands to create and remove directories:
> cd
> mkdir Ships
> ls -F
> mkdir Ships/Space
> cd ~/Ships/Space
> touch Enterprise Voyager DS9
> cd
> ls -R Ships
> mkdir ~/Ships/Sea
> cd Ships
> ls Sea
> rmdir Sea
> rmdir Space
> ls Space
> rm -ir Space #(answer Y to all prompts)
> cd ..
> pwd
> rm -r Ships
Copying Files
cp
- Makes an exact duplicate of a file.
Syntax
cp [-i] old_file new_file
cp [-i] file [file ...] directory
- If
new_file
already exists,cp
will overwrite it. - Option
-i
will prompt before overwriting the file. - If the target is a directory,
cp
will copy one or more files into that directory with the original names.
Example
> ls
file_a file_b
> cp file_a file_a.cpy
> ls
file_a file_a.cpy file_b
> cp fileb /tmp/$LOGNAME
> ls /tmp
tux
> mkdir backup
> ls -F
backup/ file_a file_a.cpy file_b
> cp * backup
cp: backup is a directory
> ls -FR
backup/ file_a file_a.cpy file_b
backup:
file_a file_a.cpy file_b
Moving and Renaming Files
mv
- Used to move a file to a new location or rename the file.
Syntax
mv [-i] old_file new_file
mv [-i] file [file ...] directory
- If
new_file
already exists,mv
will overwrite it. - Option
-i
will prompt before overwriting the file. - If the target is a directory,
mv
will move one or more files into that directory.
Example
> ls -F
personal/ phonelist
> mv phonelist addresses
> ls -F
addresses personal/
> mv addresses personal
> ls -R
personal
Removing Files
rm
- Deletes a file from a directory.
Syntax
rm [-i] file [file ...]
- Option
-i
will prompt before removing a file. - If wildcards are used to select files, ensure that the pattern matches exactly what is to be deleted.
- Can not undo the
rm
command!
Example
> ls -F
backup/ file_a file_a.cpy file_b
> rm -i file_a.cpy
rm: remove file_a.cpy (y/n)? y
> ls -F
backup/ file_a file_b