Linux Notes
Linux Notes
Standard input (channel 0) reads input Concatenate all four step files into one
from the keyboard. file in the tmp directory.
Standard output (channel 1) sends
normal output to the terminal. [user@host ~]$ cat step1.sh step2.log s
tep3 step4 > /tmp/all-four-steps-in-one
Standard error (channel 2) sends error
messages to the terminal. Construct Pipelines
If you redirect stdout to a file and the file Pipelines send the standard output from
does not exist, then the file is created. If one process to the standard input of
the file does exist and the redirection another process.
does not append to the file, then the
edirect the output of the ls -
redirection overwrites the file's contents.
t command to the head command to
> file Redirect stdout to display the first 10 lines, with the final
overwrite a file. result redirected to the /tmp/first-
>> file Redirect stdout to append to ten-changed-files file.
a file
2> Discard stderr error [user@host ~]$ ls -t | head -n 10 > /tm
/dev/null messages by redirecting p/first-ten-changed-files
them to /dev/null.
In this example, the output of
2> file Redirect stderr to overwrite
a file. the ls command goes to
the /tmp/saved-output file, and
> file 2>&1 Redirect stdout and stderr to the less command displays nothing on
overwrite the same file. the terminal.
&> file
>> file 2>&1 Redirect stdout and stderr to [user@host ~]$ ls > /tmp/saved-output |
append to the same file. less
&>> file
[user@host ~]$ ls -t | head -n 10 | tee • The :w command writes (saves)
/tmp/ten-last-changed-files the file and remains in
command mode for more
If you use the tee command at the end editing.
of a pipeline, then the terminal shows
• The :wq command writes (saves)
the output of the commands in the
the file and quits Vim.
pipeline and saves it to a file at the
• The :q! command quits Vim,
same time.
and discards all file changes
Use the tee command -a option to since the last write.
append the content to a file instead of In Vim, you can yank and put (copy
overwriting it. and paste), by using
the y and p command characters.
[user@host ~]$ ls -l | tee -a /tmp/appe Position the cursor on the first
nd-files character to select, and then enter
visual mode. Use the arrow keys to
Edit Text Files from the Shell expand the visual selection. When
Prompt ready, press y to yank the selection
You can open a file for editing by using into memory. Position the cursor at
the vi command: the new location, and then
press p to put the selection at the
[user@host ~]$ vi filename cursor.
Change the Shell Environment
Commands to see what the file contain after
editing it : Cat file1
Assign a value to a shell variable with
the following syntax:
An i keystroke enters insert mode,
where all typed text becomes file [user@host ~]$ VARIABLENAME=value
content. Pressing Esc returns to
command mode. You can use the set command to
list all shell variables that are
A v keystroke enters visual mode, currently set.
where multiple characters might be
To improve readability, you can pipe
selected for text manipulation.
the output to the less command so
Use Shift+V for multiline
that you can view it one page at a
and Ctrl+V for block selection. To exit
time.
the visual mode, use the v, Shift+V,
or Ctrl+V keystrokes.
[user@host ~]$ set | less
Use the ps command to view process very user has exactly one primary
information. The default is to show only group. For local users, this group is
processes in the current shell. Use
listed by GID in the /etc/passwd file.
the ps command -a option to view all
The primary group owns files that the
processes with a terminal. Use
the ps command -u option to view the
user creates.
user that is associated with a process. In
the following output, the first column
shows the username.
Gain Superuser Access usermod options: Usage
This example uses the su command
-c, --comment Add
from the user01 account to switch to COMMENT
the COMMENT text to
the user02 account:
the comment field.
Notice that the group name updates [root@host ~]# usermod -g group01 user0
2
to group0022 from group02.
[root@host ~]# id user02
[root@host ~]# groupadd -r group02
uid=1006(user02) gid=10000(group01) gro
[root@host ~]# tail /etc/group ups=10000(group01)
...output omitted...
Use the usermod -aG command to add
group01:x:10000:
a user to a supplementary group.
group02:x:988:
Chapter3
[user@host ~]$ command1 ; command2
command1 output • Static content remains
command2 output unchanged until explicitly edited
[user@host ~]$ or reconfigured.
[user@host ~]$ date • Dynamic or variable content
Sun Feb 27 08:32:42 PM EST 2022 might be modified or appended
by active processes.
[user@host ~]$ date +%R
• Persistent content remains after
20:33
a reboot, such as configuration
[user@host ~]$ date +%x
settings.
02/27/2022
• Runtime content from a process
or from the system is deleted
on reboot.
administration
commands
[user@host ~]$ touch Videos/blockbuster
• /usr/local: Locally 1.ogg
customized software
/var System-specific variable data
should persist between boots. [user@host Videos]$ pwd
Files that dynamically change, /home/user/Videos
such as databases, cache
[user@host Videos]$ cd .
directories, log files, printer-
[user@host Videos]$ pwd
spooled documents, and
website content, might be /home/user/Videos
[user@host Documents]$ ls -l
-rw-r--r--. 1 user user 7100 Mar 7 14
:37 thesis_chapter1.txt [user@host Documents]$ rm Thesis/Chapte
r1/thesis_chapter1.txt
-rw-r--r--. 1 user user 11431 Mar 7 14
:39 thesis_chapter2.txt [user@host Documents]$ rm Thesis/Chapte
r1
...output omitted...
rm: cannot remove 'Thesis/Chapter1': Is
[user@host Documents]$ mv thesis_chapte a directory
r2.txt thesis_chapter2_reviewed.txt
[user@host Documents]$ rm -r Thesis/Cha
[user@host Documents]$ ls -l pter1
-rw-r--r--. 1 user user 7100 Mar 7 14
:37 thesis_chapter1.txt
-rw-r--r--. 1 user user 11431 Mar 7 14 [user@host Documents]$ rm -ri Thesis
:39 thesis_chapter2_reviewed.txt
...output omitted...
If you specify both the -i and -
Use the mv command to move a file to f options, then the -f option takes
a different directory. In the next priority and you are not prompted
example, the thesis_chapter1.txt file is for confirmation before rm removes
moved from the ~/Documents directory files.
to
the ~/Documents/Thesis/Chapter1 direct You can also use the rmdir command
ory. You can use the mv command - to remove empty directories. Use
v option to display a detailed output the rm command -r option to remove
of the command operations. non-empty directories.
that point to the same data file able cast easy echo
content.
bashCopy code
cat /etc/passwd | sort -t: -k1
mkdir -p /path/to/new/directory
Breaking down the command:
If any of the directories in the path
("/path/to/new/directory") do not exist, • cat /etc/passwd: Displays the
the -p option will create them along with content of the /etc/passwd file.
the final directory specified. • |: Pipes the output of the cat
command to the next command.
The groupadd -r command in Linux is used • sort -t: -k1: Sorts the lines based
to create a system group. on the first field (username) using
: as the field separator.
The system's default umask values for [user@host ~]$ ls -ld zero
Bash shell users are defined in drwxrwxrwx. 2 user user 0 May 9 01:54
the /etc/login.defs file, and in zero
the /etc/bashrc file. Users can override
the system defaults in A umask of 027 ensures that new files
the .bash_profile or .bashrc files in their have read and write permissions for user
home directories. and read permission for group. New
directories have read and execute
If you create a regular file, then its initial permissions for group and no
octal permissions are 0666 (000 110 110 permissions for other.
110, in binary representation). Then, the
0022 umask (000 000 010 010) disables
the write permission bit for group and
others. Thus, the owner has both read
and write permission on files, and both
group and other are set to read (000 110
100 100).