0% found this document useful (0 votes)
79 views

Chap 1: Your First Keystrokes

The document provides instructions for various Linux commands organized into multiple chapters. Chapter 1 covers basic commands for viewing system information and managing files and directories. Chapter 2 discusses navigating the file system using commands like cd, pwd and ls. Chapter 3 introduces text editors like gedit, nano and vi. Chapter 4 covers commands for copying, moving and deleting files such as cp, mv and rm. The document continues teaching additional Linux commands and their usage across several chapters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Chap 1: Your First Keystrokes

The document provides instructions for various Linux commands organized into multiple chapters. Chapter 1 covers basic commands for viewing system information and managing files and directories. Chapter 2 discusses navigating the file system using commands like cd, pwd and ls. Chapter 3 introduces text editors like gedit, nano and vi. Chapter 4 covers commands for copying, moving and deleting files such as cp, mv and rm. The document continues teaching additional Linux commands and their usage across several chapters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Chap 1: Your First Keystrokes:

 date => display the calendar of current month


 cal 2021 => display calendar in 2021
 cal dec 2000
 clear => clear terminal screen
 lscpu => display ur CPU architecture information
 uptime => check how long sys has been running
 reboot => restart
 pwd => print working directory
 ls =>list the content of ur current working directory
 passwd => change password of ubuntu
 free => display the amount of free and used memory
 df => display the amount of disk space available on ur sys
 echo => print a line of text to ur Terminal
 history “, than ! <number>”
 uname, lsb_release
 exit => exit command

Chap2 : Climbing the Tree

 cd / => change directory


 pwd => represent the root directory
 man hier => read more about the Linux filesystem hierarchy
 cd /home/Elliot/Desktop => , only the second and third forward slashes are directory
separators, but the first forward slash represents the root of the filesystem.
 cat hello.txt => see the content of the textfile
 cd .. => return to the previous parent directory
 cd ~ => go to ur home directory
 whoami=>prints the name of the currently logged-in user.
 su “name of other user in computer” => switch user
 cd - => go back to your previous working directory
 touch => update both the last modification and access times of existing files
create new empty files
touch -m => update only modification times
ls -l -u => get the last access times instead of the modification times
touch -a=> only change the access times
ls -l -t => list the files sorted by modification times, newest first
ls -l -t -u => sort by access times instead
 mkdir “name of new directory” “name of new directory” => create multiple new directory
 mkdir -p dir1/dir2/dir3 => create a whole path of directories
 mkdir -p dir4/{dir5,dir6,dir7}

Chap 3: Meet the Editors


 gedit /proc/cpuinfo => displays ur CPU information
 gedit file1 => open gedit graphical editor and displays content/info of file1
 gedit /home/Elliot/cats.txt =>create new file named cats.txt
 nano => open nano editor
 vi facts.txt => open facts.txt with vi editor
 cat file1 file2 file3 => concatenate all three files
 tac fact.txt => view file in reverso content
 more /etc/services => view content of large file
 less /etc/services => like ‘more’ command but advanced
 head facts.txt => view first 10 lines of txt
 head -n 3 facts.txt => view first 3 lines of txt
 tail facts.txt => view last 10 lines of txt

Chap 4: Copying, Moving and Deleting Files:

 cp cats.txt copycat.txt => copy cats.txt named copycat.txt


 cp cats.txt /tmp => copy cats.txt to another directory
 cp cats.txt /tmp/cats2.txt => copy cats.txt to another directory with different name
cp source_file(s) destination
 copy -r cites /tmp => copy directory
 copy -r d1 d2 d3 /tmp => copy directories
 mv apple.txt banana .txt /home/Elliot/d1 => moving multiple files
mv Source_file(s) Destination
 mv d1 d2 d3 tmp => moving dir d1,d2,d3 to dir tmp
 mv cats.txt dogs.txt => rename file (new name was b4 not existed)
 mv d1 d1’ => rename directory
 mv cats.txt .cats.txt => hiden file
 rm apple.txt banana.txt => remove files
 rm -r d1 d2 d3 => remove directories

Chap 5: Read your Manuals:

 type <name of command> => category of command


 which <name of command> => determine the location of an executable command.
 whatis <name of command> => what does command do
 man <name of command> => how to use command
 help <name of command> => for shell builtins
 info <name of command> => like ‘man’
 apropos <what we need> => helps you in searching for the right command to use to
achieve a specific task.

Chap 6: Hard versus Soft Links

 ls -I cool_facts.txt => view inode number of file


 stat cool_facts.txt => list all attributes of the file
 ls -s <original_file> <soft_link> => create soft link for file
 ls -s <original_directory <soft_link> => create soft link for directory
 ls <original_file> <hard_link> => creat hard link for file
 can not creat hard link for directory

Chap 7: Who is Root?

 passwd => reset password of root


 exit => exit root
 su – root (su -) => switch to root
 su root => switch to root but the shell preserves the current user shell environment
 su => switch to root

Chap 8: Controlling the Population:


At root:

 useradd -m <name of new user> => adding user


 tail -n 1 /etc/passwd => see the user information
 passwd <name of new user> => set up password
 useradd -m -u 777 -c “Long der Drache“ -s /bin/bash Long => set up new user Long with
UID 777, comment “Long der Drache and its shell is /bin/bash
 su – Long => switch to new user
 echo $SHELL => display the user’s default shell
 usermod -u 123 -c “ok” -s /bin/bash <name of user needed to modify> => modifying
user attributes
 userdel -r <name of user> => remove the user totally
 groupadd -g 888 <name of group> => create a group with GID 888
 usermod -aG <name of group> <name of user> => add user to a group
 id <name of user> => display infos of user
 useradd -g <name of primary group> <username> => set different primary group
 usermod –gid <name of primary group> <username> => change GID of user
 groupdel <group name> =>deleting group
 chown user:group file => changing file ownership
 chmod o+w <name of file> => changing file permission for others
 echo “I am Long” >> text.txt => write in txt
 chmod o+rx /<name of directory> => changing directory permission

Chap 9 Piping and I/O Redirection:


 head -n 7 facts.txt | tail -n 4 | head -n 2
 date > mydate.txt => redirect the output of date to a file
 date 1> mydate.txt => same as above command ( 1> for stdout)
 echo “Ma r” > planet.txt => redirect the line to file
 cat blabla > error.txt => redirect error message to file (blabla was not existed b4)
 cat blabla 2> error.txt => same as above command (2> for stderr)
 rm blabla 2>> error.txt => append the file
 cat planets.txt blabla > all.txt 2>&1 => redirect output and error to a file
 cat planets.txt blabla 2> /dev/null => redirect error message to garbage collector
 read weather => stored the value entered to variable weather
 echo $weather => display the contents of variable
 read message < mydate.txt => stored the content of mydate.txt to variable

Chap 10: Analyzing and Manipulating Files


 diff <file1> <file2> => compare the contents of two files
 du -b <name of file or directory> => view size of file/dir in bytes (du-disk usage)
 wc -lwc <name of file> => display the number of lines,words,characters in file
 wc <name of file> => same as wc -lwc <name of file>
 file <name of file> => view type of file
 sort <name of file> => sort text file
 uniq <name of file> => view file without repeated lines
 sort <name of file> | uniq => omitted repeated and duplicate lines
 grep green facts.txt => display lines that have ‘green’ in it
 ls | grep .txt => view only <file>.txt
 grep -i earth facts.txt => (case sensitive by default of grep)
 sed ‘s/Sky/Cloud’ facts.txt => view content of file with replacing of Cloud for Sky
 sed -I ‘s/Sky/Cloud’ facts.txt => replace Sky by Cloud in file
 cat facts.txt | tr [:lower:] [:upper:] => view content of file in Upper characters
 cat facts.txt | tr -d ‘ ‘ | > upper.txt =>output content of file without space and
redirect output to another file
 cut -d ‘ ‘ -f1,3 facts.txt => view the first and the third word of each line
 awk ‘{print $1,$2}’ facts.txt => view the first and the second word of each line (smarter
way)
 awk ‘/red/{print}’ facts.txt => display lines that have ‘red’
wildcart
 ls -l *.txt => view all .txt file
 ls -l a* => view all file starting with ‘a’
 ls -l [af]* => view all file starting with ‘a’ or ‘f’
 ls ls [!f]*.txt => view all <file>.txt not beginning with ‘f’
 ls -l [[:digit:]]* => view all file beginning with digit
regular expressions(in text files)
 egrep d*g practice.txt => match word with zero/more occurrence start with ‘d’
and and with ‘g’
 egrep d+g practice.txt => match word with one or more occurrence start with ‘d’
and and with ‘g’
 egrep “\*” practice.txt => match special character ‘*’
 egrep m.n practice.txt => match any pattern that contains the letter ‘m’ followed
by any single character, then the letter ‘n’
 egrep ^man practice.txt => match the lines that begin with word ‘man’
 egrep man$ practice.txt => match the lines that end with word ‘man’
 egrep “[[:digit:]]{1,}” practice.txt => search for all lines that contain at least one digit
 egrep “[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{3}” practice.txt => search for pattern
3digits-3digits-3digits

Chap 11: Let’s play Find and Seek


in root
 updated
 locate <name of file> => location of the file
more powerful way: find [starting-point (s)] [options] [expression]
 find /home -name “*.txt” => search for all .txt files under ur /home directory
 find /home/elliot/dir1 -type d,f => search for all directories and file in ur /home/elliot/dir1
 find /home -name large.txt => search location of large.txt in /home
 find /root -size 0c => search by size of file (here is file with 0 character)
 find /root -name large.txt -exec rm {} + => file large.txt has been removed
Chap 12: You got a Package:
 apt-get install <package_name> => install a package
 apt-get remove <package_name> => remove a package
 apt-get purge <package_name> => remove a package with its configuration files
 apt-cache -n search <keyword> => based on key word, searching the package
 apt-cache show <package-name> => view package information
 apt-cache depends <package-name> => view dependencies of package
 dpkg -l => list all installed packages
 apt-cache pkgnames => list all packages that are available for ur install
 apt-cache pkgnames | wc -l => total number of available packages
 man sources.list => sources that system use to obtain packages
 apt-cache policy => list all the enabled repositories on
your system
 apt-cache policy <package-name> => repositories for specified package
 apt-get install –only-upgrade <package-name> => upgrade the installed package
Chap13: Kill the Process
 ps -u <username> => list all the processes that username owns
 ps -ef => list all the processes that are running on ur system
 pgrep <process_name> => get the PID(process identifiers)
 ps -fp <PID> => get the information of process
in elliot:
 firefox => open firefox as foreground
 firefox & => start up Firefox as background process
 kill -L => list all the signals that interact and communicate with processes
 kill -SIGKILL 4218 => shut down Firefox (PID is 4218)
 kill <-SIGNAL_name> <PID>
 pkill <-SIGNAL_name> <process_name> => like ‘kill’ command
in root:
 ps -o nice -p 1234 => view a process’ nice value (1234 is PID of a process)
 nice -n <from -20 to 19> <process_name> => set priority for new process
 renice -n 19 -p 1234 => changing priority of process (1234 is PID of a process)
 cd /proc/<process_name>
. ls -l exe => find a soft link named exe that points to the process'
executable file
. head status => various insightful information of process
. cat limits => displays the current limits set for the process

Chap 14: The Power of Sudo


in root:
 visudo => open file /etc/sudoers
. user hosts=(user:group) commands
ex: smurf ALL=(ALL) /usr/sbin/useradd => grant user smurf the permission to run the
useradd command
ex: smurf ALL=(ALL) NOPASSWD: /usr/sbin/useradd, /usr/bin/apt-get install terminator
=> user ‘smurf’ doesn’t get prompted to enter his password, also added the command
install the terminator package
. User_Alias Managers = smurf, bob => create user alias Manager including user smurf and
bob
. Cmnd_Alias USER_CMDs = /usr/sbin/useradd, /usr/sbin/userdel, /usr/sbin/usermod =>
create command alias USER_CMDs including the commands useradd, userdel, usermod
 Managers ALL=(ALL) USER_CMDs => grant smurf and bob the permission to run these
commands

. %group hosts=(user:group) commands

ex: %developers ALL(=ALL) NOPASSWD: ALL => grant the ‘developers’ group permission to
run any command on the system
 sudo -lU <username> => a list of the sudo commands a user can run
Chap 15: What’s wrong with the Network
 ping [options] host => testing network connectivity
ie. ping google.com
ping -c 2 10.2.0.0
 ls /sys/class/net => list the available network interfaces on system
 ip link show => view the available network interfaces
 nmcli device status => see the connection status of each network interfaces
 ifconfig <name_of _networkinterface> => checking IP address
 ip address show => list the status of all ur network interfaces
 netstat -rn =>check the IP address of ur default gateway
 ip route
 traceroute <destionation> => trace the route to any destination
ie. traceroute google.com
traceroute facebook.com
 cat /etc/resolv.conf => view the IP addres of ur DNS (domaine name system) server
 nslookup <domain_name> => to obtain the IP address of that domain name
ie. nslookup google.com
in root:
 echo > /etc/resolv.conf =>break DNS of ur ISP (internet service provider)
echo “nameserver 8.8.8.8” > /etc/resolv.conf => fixing DNS, using Google’s public DNS server
8.8.8.8
 ping <ur_hostname> => displaying ur computer’s IP address
 hostnamectl => view ur computer’s hostname
 hostnamectl set-hostname <new_hostname> => set new hostname
 ifconfig <interface_name> down => turn off internet
 ifconfig <interface_name> up => turn on internet
 systemctl restart NetworkManager =>restart all ur network interfaces

Chap16: Bash Scripting is Fun


 every bash script must do the following:
#/bin/bash
<be executable>
 chmod a+x <name_of_bashscript>.sh => all + executable
in bash script:
 read <name_variables> => get the user input
 echo “Hello $<name_variable>” => $ sign for using varibles
 var=$(command) => the syntax of command substitution
 <bash_script>.sh arg1 arg2 arg3 … => passing arguments to bash script
 if [ condition is true ]; then
do this …
fi

 if [ condition is true ]; then


do this …
elif [ condition is true ]; then
do this instead …
fi
 man test => containing all the test conditions
 for ((initialize ; condition ; increment)); do
do something
done
 for i in {1..20}; do
echo “Hello Long”
done
 for i in /home/elliot/*.doc; do
mv $i $(echo $i | cut -d. -f1).docx => moving file.doc to file.docx
done
 while [ condition is true ]; do
do something
done
 shuf -i 1-10 -n 2 => choosing 2 random integer numbers from 1-10
 until [ condition is true]; do
[commands]
done
 function_name () {
<commands>
}

Chap 17: You Need a Cron Job


 crontab -e => edit and create cron jobs
 crontab -l => list their cron jobs

Chap 18: Archiving and Compressing Files


 tar -cf <archive_name> <files> => create an archive
ex: tar -cf /root/backup/scripts.tar *.sh => create an archive in /root/backup for all bash scripts
in /home/elliot (note that first needs to change to /home/elliot)
 tar -tf <archive_name> => view the content of archive
ex : tar -tf /root/backup/scripts.tar
 tar -xf <archive_name/compressed_archive> -C <destination> => extracting archive
files
ex: tar -xf /root/backup/scripts.tar -C /root/myscrips => extracting all files in scripts.tar to
directory myscripts
 tar -czf <compressed_archive>.tar.gz <archive_name> => compressing with gzip
 tar -cjf <compressed_archive>.tar.bz2 <archive_name> => compressing with bzip2
 tar -cJf <compressed_archive>.tar.xz <archive_name> => compressing with xz
 time <command_or_program> => measuring performance
gzip is the fastest, xz is the lowest. Disk usage is opposite

Chap 19: Create your own Commands


 alias <alias_name>=”<command(s)_to _run”
ex. alias sort_files =”du -bs * | sort -rn”
alias date=”date;cal”
 vi /home/elliot/.bashrc => create permanent elliot in here
 unalias <alias_name> => just removed temporary alias

Chap 20: Everyone needs Disk Space


All your devices (mouses, hard disk, USB devices, CD-ROM,…) are located inside the /dev
directory

The terminal u are working on right now is, in fact, a device.

 w => view the name of the terminal you are connected


 lsblk => list block (which file represent your hard disk)

 fdisk [options] <device>


ex: fdisk /dev/sdb => create a new partition on the /dev/sdb

A filesystem basically dictates how data is organized and stored on a disk (or partition)
 mkfs –type [fstype] <disk_or_partition>
mkfs --type ext4 /dev/sdb1
 file -s /dev/sdb1 => verify our work
 wipefs -a /dev/sdb1 => remove a filesystem

Mounting refers to the process of attaching any filesystem or any storage device (USH flash
drives, CDs,…) to directory
The mount command only mounts a filesystem temporarily
 mount <filesystem> <mount_directory>
ex: mkdir /games
mount /dev/sdb1 /games => use the filesystem /dev/sdb1 to store our games (mount our
filesystem on the /games directory)
 mount | grep sdb1 => verify that /dev/sdb1 is mounted on /games
 df -h /games => display the filesystem disk space usage
 umount <filesystem>
ex: cd /games
umount /dev/sdb1

If you want to mount a filesystem permanently, then you need to include it in the filesystem
table file /etc/fstab
Each entry (or line) in /etc/fstab represents a different filesystem, and each line consists of the
following six fields:

So, for example, to mount our /dev/sdb1 filesystem on /games permanently, you need to include
the following line in /etc/fstab: /dev/sdb1 /games ext4 defaults 0 0
Now unmount /dev/sdb1: umount /dev/sdb1
mount /dev/sdb1 => Notice we did not specify a mount
destination this time, because the mount destination is already specified
in the /etc/fstab
 cd /games
dd if=/dev/zero of=bigGame bs=1G count=1 = > create a 1GB file named bigGame
In some unfortune situations, you may run into an issue where your system will not boot because
of a corrupted filesystem
 fsck -y /dev/sdb1 => check and repair filesystems

LVM to the rescue:


Logical Volume Manager (LVM) offers a better alternative for managing filesystems
Three layers of abstraction:

 Physical volume can be whole disks (/dev/sdb) or partitions (/dev/sdb1).


 Volume Group can be composed of one or more physical volumes.
 Multiple logical volume can belong to the same volume group. You can create filesystem on
logical volumes
 pvcreate <disk_or_partition> => create a physical volume
ex: pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3
 pvs => list all physical volumes
 vgcreate <vg_name> <PV1 PV2 PV3 …>
ex: vgcreate myvg /dev/sdb2 /dev/sdb3
 vgs => list all volume groups
 lvcreate –size 2G –name <lv_name> <vg_name>
ex: lvcreate –size 2G –name mybooks myvg
lvcreate –size 500M –name myprojects myvg
 lvs => list all logical volumes
Your logical volumes are represented in the device mapper directory /dev/mapper
 mkfs –type ext4 /dev/mapper/myvg-mybooks => create an ext4 filesystem on our mybooks
logical volume
 mkfs –type ext4 /dev/mapper/myvg-myprojects
 mkdir /books /projects
mount both filesystems:
 mount /dev/mapper/myvg-mybooks /books
 mount /dev/mapper/myvg-myprojects /projects

 Change to the /books and create a 1GB file named book1:


cd /books
dd if=/dev/zero of=book1 bs=1G count=1
dd if=/dev/zero of=book2 bs=900M count=1

Extending logical volumes:


 We can use the lvextend command to increase size of lvs:
ex: lvextend -r –size +1G /dev/mapper/myvg-mybooks
lvextend -r -l +100%FREE /dev/mapper/myvg-myprojects
 myvg ran out of space

Extending volume groups

 vgextend myvg /dev/sdb4

You might also like