07 Linux Commands
07 Linux Commands
Commands
What is a shell?
User
interface
for running
commands
Interactive
Shell language
Scripting
language
A sea of shells
+ The default shell is usually Bash
+ sh, ksh, tcsh, zsh, and fish
Checking what shell your using
$ printenv SHELL
/bin/bash
$ bash
whoami – username
id – user ID and group ID
uname – operating system name
ps – running processes
Getting
information top – resource usage
df – mounted file systems
man – reference manual
date – today's date
Working with files
mv – change file
cp – copy file rm – remove file
name or path
cd – change directory
cat – print file more – print file head – print first N tail – print last N echo – print string or
contents contents page-by- lines of file lines of file variable value
page
Compression and archiving
01 02 03
tar – archive a set zip – compress a unzip – extract
of files set of files files from a
compressed zip
archive
Networking
+ hostname – print hostname
+ ping – send packets to URL and print
response
+ ifconfig – display or configure system
network interfaces
+ curl – display contents of file at a URL
+ wget – download file from URL
Informational Commands
whoami and id (user info)
$ whoami
johndoe
$ id –u
501
$ id –u –n
johndoe
uname (os information)
$ uname
Darwin
$ uname –s –r
Darwin 20.6.0
$ uname -v
Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21
PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64
df (disk usage)
$ df –h ~
Filesystem Size Used Avail Use% Mounted on
/dev/nvme0n1p2 2.0T 744G 1.2T 40% /home
$ df –h
ps (process information)
$ ps –e
PID TTY TIME CMD
1 ?? 8:15.69 /sbin/launchd
76 ?? 0:13.27 /usr/sbin/syslogd
top (table of processes)
$ top –n 3
PID COMMAND %CPU TIME … USER …
38702 chrome 10.0 01:00.41 … johndoe …
38701 top 4.0 00:00.09 … johndoe …
38699 spotify 3.0 01:00.07 … johndoe …
echo (printing)
$ echo
$ echo Hello
Hello
$ echo "Learning Linux is fun!"
Learning Linux is fun!
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
date (date info)
$ date
Thu 16 Sep 2021 16:50:49 EDT
$ date "+%j day of %Y"
097 day of 2023
$ date "+It's %A, the %j day of %Y!"
It's Friday, the 097 day of 2023!
man (view manual)
$ man id
File and Directory
Commands
ls (list files)
$ ls
Document Downloads Music Pictures
$ ls Downloads
download1.zip
download2.zip
download3.zip
$ ls –l (shows additional info)
pwd (print working directory)
$ pwd
/Users/me
find (find files)
$ pwd
/Users/me/Documents
$ find . -name "a.txt"
./folder1/a.txt
$ find . -iname "a.txt"
./folder1/a.txt
./folder2/A.txt
File and Directory
Management Commands
mkdir (creates directories)
$ pwd
/Users/me/Documents
$ mkdir test
$ ls
rm (removes a file or directory)
$ pwd
/Users/me/Documents
$ rm file1
$ rm folder1
$ rm –r folder1 (deletes a directory with files)
$ rmdir empty_folder
touch (create an empty file)
$ pwd
/Users/me/Documents
$ touch a.txt b.txt c.txt d.txt
$ date –r notes.txt
Mon 8 Nov 2021 16:37:45 EST
$ touch notes.txt
$ date –r notes.txt
Fri 12 Nov 2021 10:46:03 EST
cp (copy)
# to copy files
$ cp /source/file /dest/filename
$ cp /source/file /dest/
# to copy directories
$ cp –r /source/dir/ /dest/dir/
mv (move)
# to move files
$ mv /source/file /dest/dir/
# to move directories
$ mv /source/dir/ /dest/dir/
chmod (managing file permissions)
$ ls –l my_script.sh
-rw-r—r- my_script.sh
$ ./my_script.sh
$ chmod +x my_script.sh
$ ls –l my_script.sh
-rwxr-xr-x my_script.sh
View File Contents
cat (catenate)
$ ls
numbers.txt
$ cat numbers.txt
more (print file page-by-page)
$ more numbers.txt
$ head –n 3 numbers.txt
tail (print last lines, 10 by default)
$ tail numbers.txt
$ tail –n 3 numbers.txt
wc (word count)
$ wc pets.txt
7 7 28 pets.txt 7 lines, 7 words, 28 characters
$ wc –l pets.txt (line count)
7 pets.txt
$ wc –w pets.txt (word count)
7 pets.txt
$ wc –c pets.txt (character count)
28 pets.txt
Wrangling Text Files
sort (sort lines in a file)
$ sort pets.txt
$ hostname eth0
ping (test server connection)
$ ping www.google.com
$ ping –c 5 www.google.com
curl (client url)
$ curl www.google.com
Archiving Compression
+ Store rarely used information + Reduces file size by reducing
and preserve it information redundancy
+ Are a collection of data files + Preserves storage space,
and directories stored as a speed up data transfer, and
single file reduces bandwidth load
+ Make the collection more
portable and serve as a backup
in case of loss or corruption
Sample directory tree
tar (tape archiver)
$ tar –cf notes.tar notes
-c means to create a tar ball
-f means to get the contents from a file
$ tar –czf notes.tar.gz notes
-z means to compress
$ tar –tf notes.tar
-t means to list the contents of a tar ball
tar (tape archiver)
$ tar –xf notes.tar notes
-x means to eXtract the tar ball
$ tar –xzf notes.tar.gz notes
zip (file compression and archiving)
zip: $ zip –r notes.zip notes
tar: