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

Linux Notes

Uploaded by

Wâlâ Marzouki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Linux Notes

Uploaded by

Wâlâ Marzouki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 5. Create, View, and Edit • &> output.

log instead of >


output.log 2>&1
Text Files
• &>> output.log instead of >>
output.log 2>&1
Redirect Output to a File or Program:
Examples for Output Redirection
Copy the last 100 lines from
the /var/log/secure file to
the /tmp/last-100-log-secure file.

[user@host ~]$ tail -n 100 /var/log/sec


ure > /tmp/last-100-log-secure

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

• The x key deletes a single


character.
If you enter instead the echo your shell prompt. Various special
$COUNT command, then it prints the character expansions that the prompt
value of the COUNT variable. supports are listed in the
"PROMPTING" section of the bash(1)
[user@host ~]$ echo $COUNT man page.
40
[user@host ~]$ PS1="bash\$ "
You can also use a variable to refer to bash$ PS1="[\u@\h \W]\$ "
a long file name for multiple [user@host ~]$
commands.
When you assign a variable in the
[user@host ~]$ file1=/tmp/tmp.z9pXW0Hqc shell, it is typically limited to the
C
current session. To make it available
[user@host ~]$ ls -l $file1 to child processes, you use the export
command. For example:

[user@host ~]$ EDITOR=vim


[user@host ~]$ echo Repeat ${COUNT}x
[user@host ~]$ export EDITOR
Repeat 40x
You can set and export a variable in
the HISTFILE, HISTFILESIZE, one step:
and HISTTIMEFORMAT shell variables
affect the shell history and
[user@host ~]$ export EDITOR=vim
the history command.
The HISTFILE variable specifies which Another important environment
file to save the shell history to, and variable is PATH. The PATH variable
defaults to the ~/.bash_history file. contains a list of colon-separated
The HISTFILESIZE variable specifies directories that contain programs:
how many commands to save in that
file from the history.
[user@host ~]$ echo $PATH
The HISTTIMEFORMAT variable defines
/home/user/.local/bin:/home/user/bin:/u
the time stamp format for every sr/share/Modules/bin:/usr/local/bin:/us
command in the history. This variable r/bin:/usr/local/sbin:/usr/sbin
does not exist by default.
Another example is the LANG variable,
which sets the locale encoding. This
Another example is the PS1 variable,
variable adjusts the preferred
which controls the appearance of the
language for program output; the
shell prompt. If you change this value,
character set; the formatting of dates,
then it changes the appearance of
numbers, and currency; and the sort
order for programs. If it is set [user@host ~]$ unset file1

to en_US.UTF-8, then the locale uses US [user@host ~]$ echo $file1


English with UTF-8 Unicode character
encoding. If it is set, for example, [user@host ~]$
to fr_FR.UTF-8, then it uses French
UTF-8 Unicode encoding. To unexport a variable without
unsetting it, use the export -
[user@host ~]$ date n command:
Tue Jan 22 16:37:45 CST 2019
[user@host ~]$ export LANG=fr_FR.UTF-8 [user@host ~]$ export -n PS1

[user@host ~]$ date


To unset an alias, use
mar. janv. 22 16:38:14 CST 2019
the unalias command:

To list all the environment variables


[user@host ~]$ unalias hello
for a shell, run the env command.
Bash aliases are shortcuts to other Chapter 6. Manage Local Users
Bash commands. For example, if you
and Groups
must often type a long command,
then you can create a shorter alias to
User accounts are of the following
invoke it. You use the alias command
main types:
to create aliases. Consider the
following example, which creates The superuser account administers
a hello alias for an echo command. the system. The superuser name
is root and the account has a UID of 0.
[user@host ~]$ alias hello='echo "Hello The superuser has full system access.
, this is a long string."'

The system user accounts are used


You can then run the hello command
by processes that provide supporting
and it invokes the echo command.
services. These processes, or daemons,
usually do not need to run as the
[user@host ~]$ hello
superuser.
Hello, this is a long string.
Most users have regular
To unset and unexport a variable, use user accounts for their day-to-day
the unset command: work. Like system users, regular users
have limited access to the system.
[user@host ~]$ echo $file1
/tmp/tmp.z9pXW0HqcC
Use the id command to show [user01@host ~]$ cat /etc/passwd
information about the currently
...output omitted...
logged-in user:
user01:x:1000:1000:User One:/home/user0
1:/bin/bash
[user01@host ~]$ id
uid=1000(user01) gid=1000(user01) group user01 : The username for this user.
s=1000(user01) context=unconfined_u:unc
onfined_r:unconfined_t:s0-s0:c0.c1023 x : The user's encrypted password was
historically stored here; it is now a
To view information about another placeholder.
user, pass the username to 1000 : The UID number for this user
the id command as an argument: account.
1000 : The GID number for this user
[user01@host ~]$ id user02 account's primary group. Groups are
uid=1002(user02) gid=1001(user02) group discussed later in this section.
s=1001(user02) context=unconfined_u:unc
onfined_r:unconfined_t:s0-s0:c0.c1023 User One : A brief comment,
description, or the real name for this
Use the ls -l command to view the user.
owner of a file. Use the ls - /home/user01 : The user's home
ld command to view the owner of a directory, and the initial working
directory, rather than the contents of directory when the login shell starts.
that directory. In the following output,
/bin/bash : The default shell program
the third column shows the username.
for this user that runs at login. Some
accounts use the /sbin/nologin shell to
[user01@host ~]$ ls -l mytextfile.txt disallow interactive logins with that
-rw-rw-r--. 1 user01 user01 0 Feb 5 11 account.
:10 mytextfile.txt
[user01@host]$ ls -ld Documents [user01@host ~]$ cat /etc/group

drwxrwxr-x. 2 user01 user01 6 Feb 5 11 ...output omitted...


:10 Documents group01:x:10000:user01,user02,user03

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.

[user01@host ~]$ su - user02 [root@servera ~]# u


sermod -c "Operator
Password: user02_password One" operator1
[user02@host ~]$
-d, --home Specify a home
If you omit the username, then HOME_DIR
directory for the
the su or su - command attempts to user account.
switch to root by default.
-g, --gid GROUP Specify the primary
[user01@host ~]$ su - group for the user
account.
Password: root_password
[root@host ~]#
-G, --groups Specify a comma-
GROUPS
separated list of
Manage Local User Accounts supplementary
The useradd username command creates groups for the user
a user called username. account.
The usermod command in Linux is -L, --lock Lock the user
used to modify user account account.
properties. -m, --move-home Move the user's
usermod options: Usage home directory to
a new location.
-a, --append Use it with the - You must use it
G option to add the
with the -d option.
supplementary
groups to the -s, --shell Specify a particular
SHELL
user's current set login shell for the
of group user account.
memberships -U, --unlock Unlock the user
instead of account.
replacing the set of
supplementary
groups with a new The userdel username command
set. removes the username user
from /etc/passwd, but leaves the user's
home directory intact. The userdel - group0022:x:988:

r username command removes the user


from /etc/passwd and deletes the The groupmod command -g option
user's home directory. specifies a new GID.

The passwd username command sets the


[root@host ~]# groupmod -g 20000 group0
initial password or changes the 022
existing password for
[root@host ~]# tail /etc/group
the username user.
...output omitted...
Manage Local Groups
group0022:x:20000:

The groupadd command -g option The groupdel command removes


specifies a GID for the group to use. groups.

[root@host ~]# groupadd -g 10000 group0 [root@host ~]# groupdel group0022


1
[root@host ~]# tail /etc/group Use the usermod -g command to
...output omitted... change a user's primary group.
group01:x:10000:
[root@host ~]# id user02
The groupadd command -r option uid=1006(user02) gid=1008(user02) group
creates system groups. s=1008(user02)

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:

[root@host ~]# id user03


The groupmod command -n option
specifies a new name for the group. uid=1007(user03) gid=1009(user03) group
s=1009(user03)
[root@host ~]# usermod -aG group01 user
[root@host ~]# groupmod -n group0022 gr 03
oup02
[root@host ~]# id user03
[root@host ~]# tail /etc/group
uid=1007(user03) gid=1009(user03) group
...output omitted... s=1009(user03),10000(group01)
Important encrypted passwords became
The usermod command -a option common. The cryptographically
enables the append mode. Without hashed passwords were moved to
the -a option, the command removes the /etc/shadow file, which only
the user from any of their current the root user can read.
supplementary groups that are not Like the /etc/passwd file, each user has
included in the -G option's list. an entry with in the /etc/shadow file. An
example entry from
A user's primary group is the group the /etc/shadow file has nine colon-
that is viewed on the user's account in separated fields:
the /etc/passwd file. A user can belong
to only one primary group at a time.
[root@host ~]# cat /etc/shadow

A user's supplementary groups are the ...output omitted...

additional groups that are configured user03:$6$CSsXsd3rwghsdfarf:17933:0:999


99:7:2:18113:
for the user and viewed on the user's
entry in the /etc/group file. A user can
Each field of this code block is
belong to as many supplementary
separated by a colon:
groups as is necessary to implement
file access and permissions effectively. • user03 : Name of the user
account.
Use the newgrp command to switch
• $6$CSsXsd3rwghsdfarf : The
your primary group, in this shell
cryptographically hashed
session.
password of the user.
[user03@host ~]# id • 17933 : The days from the epoch
uid=1007(user03) gid=1009(user03) group when the password was last
s=1009(user03),10000(group01) changed, where the epoch
[user03@host ~]$ newgrp group01 is 1970-01-01 in the UTC time
[user03@host ~]# id zone.
• 0 : The minimum days since the
uid=1007(user03) gid=10000(group01) gro
ups=1009(user03),10000(group01) last password change before
the user can change it again.
• 99999 : The maximum days

Manage User Passwords without a password change


before the password expires. An
Originally, encrypted passwords were
empty field means that the
stored in the world-
password never expires.
readable /etc/passwd file.
These passwords were considered
adequate until dictionary attacks on
• 7 : The number of days ahead to [root@host ~]# chage -l cloudadmin10 |

warn the user that their


grep "Account expires"
password will expire.
Account expires
• 2 : The number of days without : Apr 09, 2022
activity, starting with the day
that the password expired, Use the chage command -l option to
before the account is display the password aging policy for
automatically locked. the cloudadmin10 user.
• 18113 : The day when the
[root@host ~]# chage -d 0 cloudadmin10
account expires in days since
the epoch. An empty field
The next time that
means that the account never
the cloudadmin10 user logs in, the user
expires.
is prompted to change the password.
• The last field is typically empty
and is reserved for future use.
The date command can calculate a
future date. The -u option reports the
The following example time in UTC.
demonstrates the chage command to
change the password policy of [user01@host ~]$ date -d "+45 days" -u
the sysadmin05 user. The command Thu May 23 17:01:20 UTC 2019
defines a minimum age (-m) of zero
days, a maximum age (-M) of 90 days, a the usermod command -L option locks a
warning period (-W) of 7 days, and an user account and the user cannot log
inactivity period (-I) of 14 days. in to the system.

[root@host ~]# chage -m 0 -M 90 -W 7 -I [root@host ~]# usermod -L sysadmin03


14 sysadmin05
[user01@host ~]$ su - sysadmin03
Password: redhat
[root@host ~]# date +%F
su: Authentication failure
2022-03-10
[root@host ~]# date -d "+30 days" +%F In the following example,
the usermod command locks and
expires the cloudadmin10 user at 2022-
2022-04-09
08-14.
[root@host ~]# chage -E $(date -d "+30

days" +%F) cloudadmin10 [root@host ~]# usermod -L -e 2022-08-14


cloudadmin10
Chapter 2. 997 total

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.

[user@host ~]$ file /etc/passwd


Locati
on Purpose
/etc/passwd: ASCII text
/boot Files to start the boot process.
/dev Special device files that the
system uses to access
hardware.
[user@host ~]$ cat file1 file2
/etc System-specific configuration
Hello World!!
files.
Introduction to Linux commands.
/home Home directory, where
regular users store their data
and configuration files.
/root Home directory for the
[user@host ~]$ wc /etc/passwd
administrative
41 98 2338 /etc/passwd
superuser, root.
[user@host ~]$ wc -l /etc/passwd ; wc - /run Runtime data for processes
l /etc/group
that started since the last
41 /etc/passwd
boot. This data includes
63 /etc/group process ID files and lock files.
[user@host ~]$ wc -c /etc/group /etc/ho The contents of this directory
sts
are re-created on reboot. This
883 /etc/group directory consolidates
114 /etc/hosts the /var/run and /var/lock dir
Locati
on Purpose
ectories from earlier versions [user@host ~]$ pwd
of Red Hat Enterprise Linux. /home/user
/tmp A world-writable space for [user@host ~]$ ls
temporary files. Files that are
Desktop Documents Downloads Music P
not accessed, changed, or ictures Public Templates Videos
modified for 10 days are [user@host ~]$
deleted from this directory
automatically.
The /var/tmp directory is also a
[user@host ~]$ pwd
temporary directory, in which
/home/user
files that are not accessed,
changed, or modified in more [user@host ~]$ cd Videos
than 30 days are deleted [user@host Videos]$ pwd
automatically. /home/user/Videos
/usr Installed software, shared [user@host Videos]$ cd /home/user/Docum
libraries, including files, and ents
read-only program data. [user@host Documents]$ pwd
Significant subdirectories in /home/user/Documents
the /usr directory include the
[user@host Documents]$ cd
following commands:
[user@host ~]$ pwd
• /usr/bin: User
commands /home/user

• /usr/sbin: System [user@host ~]$

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

found under /var. [user@host Videos]$ cd ..


[user@host ~]$ pwd
/home/user Thesis/Chapter1:
[user@host ~]$ cd Videos
[user@host Videos]$ pwd Thesis/Chapter2:
/home/user/Videos
[user@host Videos]$ cd /home/user/Docum Thesis/Chapter3:
ents
[user@host Documents]$ pwd
/home/user/Documents The cp command copies a file, and
creates a file either in the current
[user@host Documents]$ cd -
directory or in a different specified
[user@host Videos]$ pwd
directory.
/home/user/Videos

[user@host ~]$ cd Videos


[user@host Videos]$ cp blockbuster1.ogg
blockbuster3.ogg
changes the working directory up two [user@host Videos]$ ls -l
levels from the current location: cd ../.. total 0

• ~: The tilde represents the -rw-rw-r--. 1 user user 0 Feb 8 16:


23 blockbuster1.ogg
home directory of the user.
-rw-rw-r--. 1 user user 0 Feb 8 16:
So, when you type cd ~ and press 24 blockbuster2.ogg
Enter, it is equivalent to typing cd
-rw-rw-r--. 1 user user 0 Feb 8 16:
without any arguments, 34 blockbuster3.ogg

By default, the cp command does not


[user@host Documents]$ mkdir ProjectX P copy directories; it ignores them.
rojectY ProjectZ
[user@host Documents]$ ls [user@host Documents]$ cd ProjectY
ProjectX ProjectY ProjectZ [user@host ProjectY]$ cp -r ../Thesis/
.

Use the mv command to rename a file.


[user@host Documents]$ mkdir -p Thesis/
Chapter1 Thesis/Chapter2 Thesis/Chapter In the following example, the mv
3 thesis_chapter2.txt command
[user@host Documents]$ ls -R Thesis/ renames the thesis_chapter2.txt file
Thesis/:
to thesis_chapter2_reviewed.txt in the
same directory.
Chapter1 Chapter2 Chapter3

[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.

[user@host Documents]$ ls Thesis/Chapte [user@host Documents]$ pwd


r1
/home/user/Documents
[user@host Documents]$
[user@host Documents]$ rmdir ProjectZ
[user@host Documents]$ mv -v thesis_cha
pter1.txt Thesis/Chapter1 [user@host Documents]$ rmdir ProjectX

renamed 'thesis_chapter1.txt' -> 'Thesi rmdir: failed to remove 'ProjectX': Dir


s/Chapter1/thesis_chapter1.txt' ectory not empty

[user@host Documents]$ ls Thesis/Chapte [user@host Documents]$ rm -r ProjectX


r1 [user@host Documents]$
thesis_chapter1.txt
[user@host Documents]$ ls -l
The following example creates a hard
-rw-r--r--. 1 user user 11431 Mar 7 14
:39 thesis_chapter2_reviewed.txt link called newfile-hlink2.txt for the
...output omitted...
existing newfile.txt file in
the /tmp directory.
able alfa baker bravo cast charlie
[user@host ~]$ ln newfile.txt /tmp/newf delta easy
ile-hlink2.txt
[user@host glob]$ ls [ac]*
To determine whether two files are *b* matches all file names that contain a "b"
hard linked, use the ls command - able alfa cast charlie
i option to list each file's inode
number. If the files are on the same file
system and their inode numbers are
the same, then the files are hard links [user@host glob]$ ls ????

that point to the same data file able cast easy echo
content.

[user@host ~]$ ls -il newfile.txt /tmp/ [user@host glob]$ echo {Sunday,Monday,T


newfile-hlink2.txt uesday,Wednesday}.log
8924107 -rw-rw-r--. 2 user user 12 Mar Su
11 19:19 newfile.txt
nday.log Monday.log Tuesday.log Wednesd
8924107 -rw-rw-r--. 2 user user 12 Mar ay.log
11 19:19 /tmp/newfile-hlink2.txt
[user@host glob]$ echo file{1..3}.txt
In the following example, the ln - file1.txt file2.txt file3.txt
s command creates a symbolic link for [user@host glob]$ echo file{a..c}.txt
the /home/user/newfile-link2.txt file. filea.txt fileb.txt filec.txt
The name for the symbolic link
[user@host glob]$ echo file{a,b}{1,2}.t
is /tmp/newfile-symlink.txt. xt
filea1.txt filea2.txt fileb1.txt fileb2
[user@host ~]$ ln -s /home/user/newfile .txt
-link2.txt /tmp/newfile-symlink.txt
[user@host glob]$ echo file{a{1,2},b,c}
[user@host ~]$ ls -l newfile-link2.txt .txt
/tmp/newfile-symlink.txt
filea1.txt filea2.txt fileb.txt filec.t
-rw-rw-r--. 1 user user 12 Mar 11 19:19 xt
newfile-link2.txt
lrwxrwxrwx. 1 user user 11 Mar 11 20:59
/tmp/newfile-symlink.txt -> /home/user/
newfile-link2.txt [[:upper:]]* : atches only file names
that begin with an uppercase letter.

*[[:digit:]]*: matches only file names


that contain a number.
[user@host glob]$ ls a*
able alfa [student@serverb ~]$ mkdir -p ~/Documen
ts/project_plans
[user@host glob]$ ls *a*
[student@serverb ~]$ touch \
~/Documents/project_plans/{season1,seas The output of ls -l typically looks like
on2}_project_plan.odf
this:
Use the arrow keys to position the cursor
cssCopy code
at the first character of the fourth column.
-rw-r --r-- 1 username groupname 4096 Jan 1
Enter the visual block mode by
00 : 00 filename
using Ctrl+V. Use the arrow keys to
position the cursor at the last character
and row of the fourth column. Delete the If you want to sort the output of the cat
selection by typing x. /etc/passwd command alphabetically
based on usernames, you can use the
sort command. Here's an example:

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.

find /path/to/search -mtime -7 sudo adduser -m -g 0 -p 1234 developer


find /path/to/search -type d
• -m: Create the user's home
find /path/to/search -name "*.jpg"
directory.
username:password:UID:GID:GECOS:home_dire • -g 0: Set the user's primary group
ctory:login_shell
to the root group (Group ID 0).
sort -n -t: -k4 /etc/passwd • -p 1234: Set the user's password.
However, using the -p option
the ls -l command does not directly directly to set the
display the user account information in adds the user to the root group (Group ID
0)
the same format as the /etc/passwd file.
The ls -l command is used to list cut -d : -f 1 /etc/passwd
detailed information about files and
directories in a directory. Here's a breakdown of the options:
• -d :: Specifies the delimiter, which By setting the umask value to 0, the
is a colon (:) in this case. The file permissions for other change from
fields in the /etc/passwd file are read to read and write. The directory
separated by colons. permissions for other change from
• -f 1: Specifies the field to extract, read and execute to read, write, and
and in this case, it's the first field. execute.

The command extracts the first field [user@host ~]$ umask 0


(username) from each line in the
[user@host ~]$ touch zero.txt
/etc/passwd file using a colon ( :) as the
delimiter. This effectively provides a list [user@host ~]$ ls -l zero.txt

of usernames on the system. -rw-rw-rw-. 1 user user 0 May 9 01:54


zero.txt

Chapter7 [user@host ~]$ mkdir zero

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).

If you create a directory, then its initial


octal permissions are 0777 (000 111 111
111). Then, the 0022 umask (000 000 010
010) disables the write permission bit for
group and other. Thus, the owner has
read, write, and execute permissions on
directories, and both group and other are
set for read and execution (000 111 101
101).

You might also like