; $pwd;ls Execute the right command regardless of whether the left command causes an error or succeeds.
&& $ls prog/ruby && pwd Execute the second command only when the first command completes successfully
The second command is executed only when the first command does not complete successfully.
() $(date;pwd;ls)>kekka.log Execute commands together
$echo $DATE > August 31 $echo '$DATE' >$DATE It is output as a character string.
$echo $DATE > August 31 $ echo "Today's date is $ DATE"
Today's date is August 31st. ''Same as. The variable part of $ is output as a variable.
$ echo "The current directory is'pwd'."
The current directory is / home / lpic.
$ echo "The current directory is $ (pwd)'."
The current directory is / home / lpic.
The command result can be output. It is recommended because it is easier to understand how to write below.
less
| Key operation | Explanation |
|---|---|
| Space bar | Scroll down one screen |
| b key | Scroll up one screen |
| / | Downward search |
| ? | Upward search |
ls | wc -l Pass the execution result of the ls command to the standard input of wc.
dmesg|less Pass kernel messages to less standard input.
$ls -l > filelist Put the display result in the filelist.
$ls -l >> filelist Added display result to the end of filelist.
< $grep "lpic" <target.txt>result.txt Standard input
$cat > sample.txt <<EOF Output until EOF is input.
2> $find / -name "*.tmp" 2> error.log Save only error output to a file.
cat cat file1 file2 > newfile Application: Write file1 file2 to newfile continuously
| option | Explanation |
|---|---|
| -n | Display with line numbers |
nl Display text files with line numbers.
tail Display 10 lines from the last line.
| option | Explanation |
|---|---|
| -n number of lines | Display only the specified number of lines |
| -f | Real-time display. Update when added during display.(ctrl+End with c) |
tr $cat /etc/hosts | tr 'a-z' 'A-Z' Convert all lowercase letters in the file to uppercase and cat
split $split -100 sample.txt s_sample.
sample.txt s_sample.aa s_sample.ab s_sample.ac
Separate sample.txt into other files every 100 lines. (File names end with aa, ab, ac ...)
| command | Explanation |
|---|---|
| md5sum | Output hash value by MD5 |
| sha1sum | |
| sha256sum | |
| sha512sum |
$sha1sum sample.txt Show SHA1 hash value of sample.txt file
grep $grep -i ab sample.txt
$grep -v '^#' /etc/httpd/conf/httpd.conf Displayed by omitting the line with # at the beginning (comment out). (^: Regular expression for the first character)
| option | Explanation |
|---|---|
| -i | Case insensitive search |
| -v | Show lines where patterns do not match |
vi
Input mode
| command | Explanation |
|---|---|
| i | Enter text before the cursor |
| a | Enter text after the cursor |
| I | Move the cursor to the first character at the beginning of a line and enter text just before it |
| A | Move the cursor to the end of the line and enter the text immediately after |
| o | Insert a blank line below the current line and enter text on that line |
| O | Insert a blank line above the current line and enter text on that line |
Command mode
| command | Explanation |
|---|---|
| h | Move one character to the left |
| l | Move one character to the right |
| k | Move up one line |
| j | Move down one line |
| o | Move to the beginning of the line |
| $ | Move to the end of the line |
| H | Move to the beginning of the line at the top of the screen |
| L | Move to the beginning of the line at the bottom of the screen |
| gg | Move to the first line of the file |
| G | Move to the last line of the file |
| nG | Move to line n of the file |
| :n | Move to line n of the file |
Move 5 characters to the left in 5h
| command | Explanation |
|---|---|
| :q | Exit without saving to file |
| :q! | Exit without saving what you are editing |
| :wq | Save the content being edited and exit |
| :w | Overwrite and save the file with the content being edited |
| :e! | Restore to the last saved content |
| :!command | Execute shell command without exiting vi |
| :r!command | Insert the execution result of the shell command |
:! You can execute the ls command with ls running vi.
vi edit command
| command | Explanation |
|---|---|
| dd | Delete current line |
| dw | Delete from one cursor to the next word |
| yy | Copy the current line to the buffer |
| :w | Overwrite and save the file with the content being edited |
| p | Paste the contents of the buffer below the current line |
| P | Paste the contents of the buffer above the current line |
| x | Delete the character at the cursor position(delete) |
| X | Delete the character before the cursor position(backspace) |
Delete 5 lines from the cursor position with 5dd.
vi search command
| command | Explanation |
|---|---|
| / | Search backward from the cursor position |
| ? | Search backward from the cursor position |
| n | Search for |
| N | Search for(Reverse direction) |
| :noh | Unhighlight candidates |
| :%s/A/B/ | Replace the first found string A with the string B |
| :%s/A/B/g | Replace all strings A with string B |
| command | Explanation |
|---|---|
| :set nu | Display line numbers |
| :set nonu | Hide line number h |
| :set ts=Tab width | Tab widthを数値で指定する |
gzip
| command | Explanation |
|---|---|
| -d | Extract the compressed file |
| -c | Output to standard output |
| -r | Compress all files in the directory |
$gzip datafile Compress file
$gzip -r sampled Compress all files in the sampled directory individually
$gzip -c datafile >datafile.gz Compressed leaving the folder before compression
$bzip2 datafile bzip takes longer than gzip but has higher compression efficiency. (Add option -d for decompression)
$xz datafile xz takes longer than bzip but has higher compression efficiency. (Add option -d for decompression)
zcat,bzcat,xzcat $zcat sample.gz Check the compressed text file without decompressing it.
tar You can combine files and directories into a single archive file and unpack it. Same as gzip compression with z option.
| command | Explanation |
|---|---|
| -c | Create an archive |
| -x | Extract files from archive |
| -t | Check the contents of the archive |
| -f file name | Specify the archive file name |
| -z | gzip compression/Deployment |
| -j | bzip2 compression/Deployment |
| -J | xz compression/Deployment |
| -v | Display detailed information |
| -u | Add only newer files with the same name in the archive |
| -r | Add files to the archive |
| -N | Only targets data newer than the specified date |
| -M | Split into multiple devices |
| -delete | Delete files from the archive |
tar cvf /dev/st0 /home Create / home archive file in / dev / st0
tar xvzf software.tar.gz Extract the archive file to the current directory.
tar tf /dev/sdb1 View the contents of the archive located in / dev / sb1.
tar xvf /dev/sdb1 var/log/secure /var
cpio $ls | cpio -o >/tmp/backup Back up the current directory as a / tmp / backup file.
dd $dd if=/dev/sdb of=/dev/sdc Output the contents of the disk connected to / dev / sdb to / dev / sdb as it is.
The dd command can handle the contents of devices such as hard disks and CD-ROMs as they are. The cp command just copies the file.
| command | Explanation |
|---|---|
| -if | Specify the input file |
| -of | Specify the output file |
chmod
| option | Explanation |
|---|---|
| -R | Change the permissions of all files under the specified directory |
| Target | Explanation |
|---|---|
| u | owner |
| g | group |
| o | Other users |
| a | All users |
| operation | Explanation |
|---|---|
| + | Add permissions |
| - | Remove permissions |
| = | Specify permissions |
| Type of permission | Explanation |
|---|---|
| r | Read permission |
| w | Write permission |
| x | Execution permission |
| s | SUID or SGID |
| t | Sticky bit |
$chmod go+w samplefile Give write permission to groups and other users
$chmod o-rw samplefile Eliminate write execution authority from other users
$chmod 644 samplefile Changed to 644
SUID,SGID Can be executed with the privileges of the owner of the SUID file
chmod u+s samplefile
SGID group access rights apply.
Even if you have write permission, you cannot delete files owned by users other than yourself.
chmod o+t sampledir
umask The default access right is set.
$umask 0002
The default is 777 for directories and 666 for files minus umask.
d→775 file→664
chown
| option | Explanation |
|---|---|
| -R | Change the owner of the specified directory and all files in it |
Refer to the same entity in each file. $ln file.original file.link_hard
Indicates the location under the link. (Shortcut) $ln -s file.original file.link_hard
ls -l Check the created link.
Add d to the copy of the symbolic link.
cp -d file.link_sym file.link3
If not added, the link source file will be copied. cp file.link_sym file.link2
top Process monitoring End with Q
updatedb & You can execute the process in the background by adding &.
jobs You can check the running jobs with the jobs command.
I want you to keep doing it when you leave your seat for a long time. If you want it to run even if you log out. Add nohub. $nohup updatedb &
If you want to run the currently running job in the background. Pause with ctrl + z → bg 1 (1 is the job number, jobs) If you want to change the background to the foreground, fg
free Memory availability
| option | Explanation |
|---|---|
| -m | Display in MB |
| -s seconds | Continue to display at specified intervals |
uptime Average load etc.
uname OS check
uname -a Detail View
watch watch -n 10 uptime Run uptime every 10 seconds.
nice Specify the priority when executing the command. $nice -n -10 updatedb
find $find /root -name "*.txt" Search by file name
| Search formula | Explanation |
|---|---|
| -name file name | Search by file name |
| -atime date and time | Search by last access time |
| -mtime date and time | Search by last update time |
| -perm access | Search by access right |
| -size size | Search by file size |
| -type File type | Search by file type f:File l:Symbolic d:directory |
| -user username | Search by file owner |
| Show matched files | |
| -exec command || ; | Execute command for matching files |
| -ok command || ¥; | Execute command for matching files(With confirmation) |
$find /data -type f -mtime -1 Search for files updated within the last day
$find /usr/bin -type f -perm -u+s Search for files with SUID under the / usr / bin directory.
$find /tmp -user student Find the file directory whose owner is student.
$find -atime +30 -exec rm {}¥; Find and delete files that have not been accessed for more than 30 days.
$alias ls='ls -l' You can give the command an alias. $alias lsless='ls -l|less'
$unalias lsless Release the set alias
$function lslink(){ls -l|grep '^l';} The lslink function that displays only symbolic links (starting with l)
$function lslink(){ls -l $1 | grep '^1';} Argument with $ 1. You can put a directory etc. in the argument.
$declare -f lslink Show only defined functions.
unset lslink Removed lslink function.
| File | Explanation |
|---|---|
| /etc/profile | Executed at login and referenced by all users |
| /etc/bash.basic | Executed when bash is started and referenced by all users |
| /etc/bashrc | ~/.Referenced from bashrc. Referenced from the time bash is started. |
| ~/.bash_profile | Executed at login |
| ~/.bash_login | ~/.bash_If there is no profile, it will be executed at login |
| ~/.profile | ~/.bash_profile too~/.bash_If you don't have login, run at login |
| ~/.bashrc | Executed when bash starts |
| ~/.bash_logout | Runs when logging out |
Execute (requires read permission) $bash lsld
$source lsld
$. lsld
(Execution authority required) chmod a+x lsld $./lsls It is executed with the file name as it is.
| Variable name | Explanation |
|---|---|
| $0 | Shell script file name(full path) |
| $1 | 1st argument |
| $2 | The second argument. Below $ 3 $ 4 $ 5 |
| $# | Number of arguments. |
| $@ | All arguments(Space delimited) |
| $* | All arguments(The delimiter is the one specified by the environment variable IFS) |
Normal termination → 0 Abnormal termination → other than 0
$echo $? 2
The return value is assigned to $ ?.
test command
| Conditional expression | Execution result |
|---|---|
| File format test | |
| -f file | (Excluding directories etc.)True if there is a file |
| -d directory | True if there is a directory |
| -r file | True if the file exists and is readable |
| -w file | True if the file exists and is writable |
| -x file | True if the file exists and is executable |
| -s file | True if there are files larger than 0 |
| -L file | True if there is a file that is a symbolic link |
| File property testing | |
| -e file | True if there is a file |
| File 1-nt file 2 | True if file 1 has a newer modification time than file 2 |
| File 1-ot file 2 | True if file 1 is older than file 2 |
| Numerical test | |
| Number 1-eq number 2 | True if number 1 and number 2 are equal |
| Number 1-ge number 2 | True if number 1 is greater than or equal to number 2. |
| Number 1-gt number 2 | True if number 1 is greater than number 2 |
| Number 1-le number 2 | True if number 1 is less than or equal to number 2 |
| Number 1-lt number 2 | True if number 1 is less than or equal to number 2 |
| Number 1-ne number 2 | True if number 1 and number 2 are not equal |
| String test | |
| -n string | True if the length of the string is greater than 0 |
| -z string | True if the length of the string is 0 |
| String 1=String 2 | True if the two strings are equal |
| String 1!=String 2 | True if the two strings are not equal |
| Logical join of tests | |
| !conditions | conditionsが偽であれば真 |
| Condition 1-a Condition 2 | True if both conditional expressions are true(and) |
| Condition 1-o Condition 2 | True if any conditional expression is true(or) |
if conditional expression then Execution statement 1 else Execution statement 2 fi
Case case $1 in 1)echo "January";; 2)echo "February";; esac
read You can hear the name from the standard input. echo -n "Who are you?:" read username ← Ask the user here echo "Hello, $ username!" ← Used here
First line of shell script #!/bin/bash
File for which user account is set /etc/passwd
group /etc/group
useradd useradd -c "Linux User" -d /home/linux -s /bin/bash linuxuser
| option | Execution result |
|---|---|
| -c comment | Specify comment field |
| -d directory | Specify home directory |
| -s path | Specify default shell |
cron
crontab It is located under the / var / spool / cron directory.
| option | Execution result |
|---|---|
| -e | Edit crontab(Do not open directly with vi etc.) |
| -l | Content display |
| -r | File deletion |
| -i | Confirm when deleting |
| -u username | Edit the crontab file with a username(root user only) |
Format Minute, hour, day, month, day of the week command
| field | Execution result |
|---|---|
| Day of the week | 0-Integer up to 7(0,7:Sunday to 6th Saturday) |
15 23 * * * /usr/local/bin/backup (Starts every day at 23:15)
0 9,12 * * 1 /usr/local/bin/syscheck (Starts at 09:00 and 12:00 on Mondays)
0 */2 * * * /usr/local/bin/syscheck (Start every 2 hours)
at One-time scheduling $at 5:00 tomorrow at>/usr/local/sbin/backup at> ^ D ← Enter ctrl + D key to finish.
systemd It can be activated after a certain period of time from some event.
mail [-s subject] [destination email address or user name]
mail -s samplemail student Hello! Student! ← Body . EOT
mail Check received email
ping
| option | Execution result |
|---|---|
| -c times | Send packets a specified number of times |
| -i interval | Specified interval(Seconds)Send every pocket |
traceroute Displays the route through which the packet travels to the specified host.
hostname Display the current host name.
If you specify a host name, change the host name.
netstat Display various information about the network. Checking open ports, etc.
nc Cat command on the network
$nc -l -p 12345 -o listen.log It listens on port 12345 and outputs the received data to the listen.log file.
$nc centos7.example.com 12345<data.txt Output the contents of the data.txt file to the 12345 port of the host.
ifconfig Confirmation of ip address.
/etc/resolv.conf
/etc/nsswitch.conf Select the order of prioritizing hosts or DNS server.
host Know the host ip. Know the host of ip.
nmap
$nmap www.example.net You can check the open port number and service name (ssh, http).
Check regularly. #find / -perm -u+s -ls
#touch /etc/nologin By creating a nologin file, logins other than the root are prohibited.
scp scp copy source file name [user name @] copy destination host: [copy destination file name]
scp /etc/hosts sv3.example.jp:/tmp Copy / etx / hosts on the local host to / tmp on the remote host sv3.example.jp.
scp [user name @] copy source host: copy source file name copy destination file name
$scp sv3.example.jp:/etc/hosts . Copy / etc / hosts of remote host sv3.example.jp to the current directory.
$scp data.txt [email protected]: Copy the data.txt file of the local host to the home directory of the fred user of the remote host sv3.example.jp
Recommended Posts