0% found this document useful (0 votes)
17 views56 pages

07 Linux Commands

Linux commands provide a way for users to interact with and get information from an operating system. The default shell is usually Bash and common shells include sh, ksh, tcsh, zsh, and fish. Basic commands like whoami, id, uname, ps, top, df, date, man provide user, system, process, disk usage and help information. Commands like ls, pwd, find, mkdir, rmdir allow navigating and managing directories. Other commands like cat, more, head, tail allow viewing file contents. Networking commands include hostname, ping, ifconfig, curl, wget. File archiving uses tar and zip while compression uses gzip and unzip.

Uploaded by

kertsolas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
17 views56 pages

07 Linux Commands

Linux commands provide a way for users to interact with and get information from an operating system. The default shell is usually Bash and common shells include sh, ksh, tcsh, zsh, and fish. Basic commands like whoami, id, uname, ps, top, df, date, man provide user, system, process, disk usage and help information. Commands like ls, pwd, find, mkdir, rmdir allow navigating and managing directories. Other commands like cat, more, head, tail allow viewing file contents. Networking commands include hostname, ping, ifconfig, curl, wget. File archiving uses tar and zip while compression uses gzip and unzip.

Uploaded by

kertsolas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 56

Linux

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

touch – create chmod – wc – get count of


empty file, update change/modify file lines, words,
file timestamp permissions characters in file

grep – return lines


in file matching
pattern
Navigating and working with
directories
ls – list files and directories

find – find files in directory tree

pwd – get present working directory

mkdir – make directory

cd – change directory

rmdir – remove directory


Printing file and string contents

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

Use space bar to go to next page


Use q + enter to go back to prompt
head (print first lines, 10 by default)
$ head 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

$ sort –r pets.txt (reversed)


uniq (unique)
$ cat pets.txt
cat
cat
cat
cat
dog
dog
cat
$ uniq pets.txt
grep ("global regular expression print")
$ cat people.txt
Alan Turing
Bjarne Stroustrup
Charles Babbage
Dennis Ritchie
Erwin Schrodinger
Fred Hoyle
Guido Rossum
Henri Poincare
Ivan Pavlov
John Neumann
Ken Thompson
$ grep ch people.txt
$ grep -i ch people.txt (case insensitive)
cut (extracts a section from each line)
$ cat people.txt
Alan Turing
Bjarne Stroustrup
Charles Babbage
Dennis Ritchie
Erwin Schrodinger
Fred Hoyle
Guido Rossum
Henri Poincare
Ivan Pavlov
John Neumann
Ken Thompson
$ cut –c 2-9 people.txt
$ cut –d ' ' -f2 people.txt
paste (merge lines from different files)
$ paste first.txt last.txt yob.txt
$ paste –d "," first.txt last.txt yob.txt
Networking Commands
hostname (print host name)
$ hostname
my-linux-machine.local
$ hostname –s
my-linux-machine
$ hostname -I
127.0.0.1
ifconfig (interface configuration)
$ ifconfig

$ hostname eth0
ping (test server connection)
$ ping www.google.com

$ ping –c 5 www.google.com
curl (client url)
$ curl www.google.com

$ curl www.google.com -o google.txt


$ head –n 1 google.txt
wget (web get, download file)
$ wget https://www.w3.org/TR/PNG/iso_8859-1.txt
$ head -n 12 iso_8859-1.txt
File Archiving and
Compression Commands
Archiving vs. Compression

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

Compress -> Bundle

tar:

Bundle -> Compress


unzip (extract and decompress a zip)
$ unzip notes.zip
The End

You might also like