Day 4 Notes on “Learn Linux in 5 Days and Level Up Your Career” by Jason Cannon on Udemy

Day 4

Deleting, copying, moving and renaming files:

Deleting:

rm file – Remove file.
rm -r dir – Remove the directory and its contents recursively.
rm -f file – Force removal and never prompt for confirmation.

rm can be used against search patterns, so for example, rm *s would delete all files starting with the character ‘s’.

Copying:

cp source_file destination_file – Copy source_file to destination_file.
cp source_file [source_file1 ...] destination_directory – Copy source_files to destination_directory.

cp in interactive mode:

cp -i – Run in interactive mode.
cp -r source_directory destination_directory – Copy source_directory recursively to destination_directory.

diff file1 file2 – Check if there are differences between the files.
cp file1 file2 file3 dir – Copies file1, file2 and file3 from the current directory to dir.
tree dir – Show the folder hierarchy as a tree.

Move or rename files:

mv – Move or rename file or directory.
mv source destination – Rename source to destination.
mv -i source_file destination_directory – Move source_file to destination_directory.

Sort options:

sort -k F – Sort by key. F is the field number. For example sort -k2 for field number 2.
sort -r – Sort in reverse order.
sort -u – Sort unique. Remove duplicate lines and only provides unique results.

Archive a collection of files:

tar [-] c|x|t f tarfile [pattern] - Create, extract or list contents of a tar archive using pattern, if supplied. Options do not need a'-'.

tar [-] c – Create a tar archive.
tar [-] x – Extract files from the archive.
tar [-] t – Display the table of contents (list).
tar [-] v – Be verbose.
tar [-] z – Use compression.
tar [-] f file – Use this file.

For example: tar cf name_of_archive_to_be.tar name_of_dir_to_archive.

Compressing files:

gzip – Compress files.
gunzip – Uncompress files.
gzcat – Concatenates compressed files.
zcat – Concatenates compress files.

Disk usage:

du – Estimates file usage.
du -k – Display sizes in Kilobytes.
du -h – Display sizes in human readable format.

Wildcards:

What is a wildcard?
1. A character or string used for pattern matching.
2. Globbing expands the wildcard pattern into a list of files and/or directories. (paths)
3. Wildcards can be used with most commands. (For example ls, rm, cp …)

* – matches zero or more characters.
a*
a*.txt
? – matches exactly one character.
?.txt
a?
a?.txt

ls ?? – Show all files with a name consisting of exactly two characters.

[] – A character class.
For example ca[nt]*, would show “can”, “cat”, “candy”, “catch”, …

[!] – Matches any of the characters NOT included between the brackets. Matches exactly one character.
For example [!aeiou]*, would show “baseball”, “cricket”, …

[a-g]* – Matches the range between ‘a’ and ‘g’.

[[:alpha:]] – Alphabetic letters.
[[:alnum:]] – Alphanumeric characters, matches alpha and digits.
[[:digit:]] – Numbers from 0 to 9.
[[:lower:]] – Lower case.
[[:space:]] – Characters such as spaces, tabs or new line characters.
[[:upper:]] – Only upper case characters.

\ – Escape character. Use if you want to match a wildcard character.

clear – Clear the console.

Input, output, and redirection:

Input/Output Types:

Abbreviation – I/O Name – File Descriptor
stdin – Standard Input – 0
stdout – Standard Ouput – 1
stderr – Standard Error – 2

> – Redirects standard output to a file. Overwrites (truncating) existing contents.
>> – Redirects standard output to a file. Appends to any existing contents.
< – Redirects input from a file to a command.
& – Used with redirection to signal that a file descriptor is being used.
2>&1 – Combines stderr and standard output.
2>file – Redirect standard error to a file.
>/dev/null – Redirect output to nowhere.

For example: ls -l
Let’s redirect this output to a file like this:
ls -l > file.txt
To check the content of file.txt, type:
cat file.txt
ls >> file.txt – Appending to file.txt.

sort < file.txt – Take the content of file.txt and redirect it to sort.

A combination of input and output from the command to file and the other way around:
sort sorted_file.txt – Sort the the content of files.txt and save the result into sorted_file.txt.

To direct an error message to a file:
ls file.txt not-here 2> out.err
cat out.err will display something like: ls: cannot access not-here: No such file or directory.

Comparing the contents of files:

diff file1 file2 – Compare two files.
sdiff file1 file2 – Side-by-side comparison.
vimdiff file1 file2 – Highlight differences in vim.

vimdiff:

Ctrl-w w – Go to next window.
:q – Quit (close current window).
:qa – Quit all (close both files).
:qa! – Force quit all.

echo new last line >> file.txt – Append “new last line” to the file.txt.

Seach contents of files:

grep – Display lines matching a pattern.
grep pattern file

grep options:

-i – Perform a search, ignoring case.
-c – Count the number of occurrences in a file.
-n – Precede output with line numbers.
-v – Invert match. Print lines that don’t match.

strings – Display printable strings.

What are pipes?

Vertical bar ‘|’ is a pipe.

cat file | grep pattern is the same as grep pattern file.

cat track.mp3 – Shows the original text in the file track.mp3.

strings track.mp3 – Shows the human-readable text in the file track.mp3.

And then use strings track.mp3 | grep -i john to search for “john” in the human-readable text of the track.mp3 file.

strings track.mp3 | grep -i john | head -1 – Display the first line of human-readable “john” search output.

grep bob /etc/password | cut -d: -f1,5 – Cut field 1 to 5 of the passwords.

grep bob /etc/password | cut -d: -f1,5 | sort – Cut field 1 to 5 of the passwords and sort the output.

grep bob /etc/password | cut -d: -f1,5 | sort | tr ":" " " – Cut field 1 to 5 of the passwords and sort the output, but not with a ‘:’ seperator, just with a space seperator ‘ ‘.

grep bob /etc/password | cut -d: -f1,5 | sort | tr ":" " " | columnn -t – Cut field 1 to 5 of the passwords and sort the output, but not with a ‘:’ seperator, just with a space seperator ‘ ‘, in a table format.

Piping output to a pager:

| less/more
cat /etc/password | less – Show content of a file, but not exceed the console.

Copy files over the network:

Command line clients:

SCP – Secure copy.
SFTP – SSH file transfer protocol.
PuTTY Secure Copy client – pscp.exe
PuTTY Secure File Transfer client – psftp.exe

Graphical SCP/SFTP clients:

Cyberduck
FileZilla
WinSCP

scp/sftp command line utilities:

scp source destination – Copy source to destination.
sftp host – Start a secure file transfer session with host.
sftp philipp@host
ftp host – Start a file transfer session with host.

For security reasons, if given the choice use scp or sftp.

An example session could look like this:
sftp linuxsvr
pwd – To show in which directory we are (should be home directory).
ls – Will show the files on the server.
lpwd – Show local working directory.
lls – Shows files that are on the local box.
put z.txt – Put z.txt onto the remote server.
rm z.txt – Remove z.txt from the remote server.

To do this with the scp command.
command, file, server name, directory on server
scp z.txt linunxsrv:/tmp/

As a different user:
scp z.txt adminuser@linuxsvr:/home/adminuser/

In FileZilla, a graphical interface:

Host: sftp://linuxsvr
Username: adminuser
Password: password

Drag and drop z.txt from local to the remote server.

Customize shell prompt (bash):

\d – Date in “Weekday Month Date” format “Tue May 26”.
\h – Hostname up to the first period.
\H – Hostname.
\n – Newline.
\t – Current time in 24-hour HH:MM:SS format.
\T – Current time in 12-hour HH:MM:SS format.
\@ – Current time in 12-hour am/pm format.
\A – Current time in 24-hour HH:MM format.
\u – Username of the current user.
\W – Basename of the current working directory.
\$ – If the effective UID is 0, a#, otherwise a $.

For a complete listing of all the formatting options see the bash man page.

echo 'export PS1="[\u@\h \w]\$ "' >> ~/.bash_profile – Append to the bash_profile.

echo $PS1 – Display current prompt string. Some destributions use prompt instead of PS1.
PS1="\$ " – Add time to the prompt.

To keep those changes between logins:
vi .bash_profile – Edit .bash_profile.

Add:

#My custom prompt
export PS1="[persist \u@\h \w]\$ "

Confirm and save file with :wq

A multi-line prompt example: PS1=”\t\n[\h \w]\$ ”
The prompt will look something like this:

09:25:06
[linuxsvr ~]$

Aliases:

What are aliases? – Shortcuts.
Use for long commands.
Use for commands you type often.

alias – Display list of current aliases set.
alias [name[=value]] – To create an alias.
unalias name – Remove the “name” alias.
unalias -a – Remove all aliases.

Fix typo:
alias grpe='grep'

Make Linux behave like another OS:
alias cls='clear' – In windows cls clears the screen.

Add aliases to your personal initialization files, for example .bash_profile, like done with prompt customizations.

For example, make an alias called “ll” to be the same as the command ls -l like this:
alias ll='ls -l'

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply

Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124