1>&2
redirectstdout
tostderr
,1
isstdout
and2
isstderr
.!!
Execute last command again.$?
expands to the exit status of the most recently executed foreground pipeline. In simpler terms, it's the exit status of the last command.- If the value of
$?
is 0, then the previous process did terminate normally (or successfully). Otherwise there was some error.
- If the value of
$0
: The filename of the current script.$#
: The number of arguments supplied to a script.$$
: The process number of the current shell. For shell scripts, this is the process ID under which they are executing.$@
expands into a list of separate parameters. Whereas,$*
is one parameter consisting of all the parameters added together as one string. Read thisyourcommand &> filename
redirects bothstdout
andstderr
fromyourcommand
to filename.$(command)
returns the output ofcommand
which stands for command substitution- You can also use
`command`
which is the same, shell might not support
- You can also use
- If you are in root folder
/
, you needsudo
to create a file. However, following command will not worksudo echo "hello" > file
will give you permission error since it needs to create thefile
first to complete redirection,sudo echo "hello" | sudo > file
is the same since we are givingsudo
to>
.- You can use
tee
to solve the problemtee [OPTION]... [FILE]...
copy standard input to each FILE and also to standard output echo "hello" | sudo tee file
- The option-string tells getopts which options to expect and which of them must have an argument. The syntax is very simple every option character is simply named as is, this example-string would tell getopts to look for -f, -A and -x:
getopts fAx VARNAME
- When you want getopts to expect an argument for an option, just place a : (colon) after the proper option flag. If you want -A to expect an argument (i.e. to become -A SOMETHING) just do:
getopts fA:x VARNAME
- When using the command shell, prefixing a command with nohup prevents the command from being aborted automatically when you log out or exit the shell.
- The name nohup stands for "no hangup." The hangup (HUP) signal, which is normally sent to a process to inform it that the user has logged off (or "hung up"), is intercepted by nohup, allowing the process to continue running.
ps aux
to show running processesps aux | egrep "your_key_word | PID"
to grep your desired process and show headerkill your_process_PID
default kill method, if not working, trykill -KILL your_process_PID
- specify the size the screen:
more -u5 <filename>
- start from a certain line number:
more +u6 <filename>
- they can be combined:
more +u3 -u2 <filename>
show 2 lines starting from line 3 - start from a certain line of text:
more +/"the text to search for" <filename>
- display current line number by pressing the euqal key (
=
) - search text
/hello world
find the first ocurrence of text "hello world"- If you want to find the 5th occurrence of "hello world" use "5/"hello world""
- Pressing the 'n' key will find the next occurrence of the previous search term. If you used a number prior to the search term that will take precedence. So if you searched for the 5th occurrence of "hello world" then pressing "n" will look for the next 5th occurrence of "hello world".
- Pressing the apostrophe (') key will go to the place where the search started.
less
: If you want to read through a large text file it is better to use the less command over an editor as it doesn't load the entire thing into memory. It loads each page into memory a page at a time making it more efficient.
ps -ef | less
show a list of running processes one page at a time.- You can change the number of lines that are scrolled when you press space key of
f
key by pressing the number immediately before pressing the key.- To make this number default, you can enter the number followed by the
z
key. F
will keep trying even if the end of the file is reached. This is helpful for an updating file.
- To make this number default, you can enter the number followed by the
b
orw
scroll one window back- Press ESC immediately prior to the space bar, allow you to continue scrolling when you have reached the end of the output.
return
,j
ore
allow you scroll one line at a time.k
ory
scroll one line back at a time.r
to repaint the screen orR
to repaint the screen discarding any output that has been buffered.g
to the beginning of the output,G
to the end of the output. number followed by%
orp
go to a certain percentage of the file.m
followed by a lowercase letter to set a marker, single quote to go to a marker- search for a pattern use a forward slash key
- load a new file into the output
:e myfile.txt
tail -n20 <filename>
specify the number of lines to seetail -n+20 <filename>
speicify the starting line to see- monitor log file
- check how the log changes every so many seconds:
tail -F -s20 <filename>
- continue monitoring until a process dies:
tail -F --pid=1234 <filename>
- to find the process id you can:
ps -ef | grep <programName>
- check how the log changes every so many seconds:
- display multiple files' contents:
cat file1_path file2_path
- create a file called test and type desired test
cat >test
some test contents
- CTRL + d
- display line numbers in file:
cat -n test
- display
$
at the end of line and also in space if there is any gap between paragraphs:cat -e test
- overwrite
test1
by the content oftest
:cat test > test1
- append the content of
test
to the end oftest1
:cat test >> test1
- redirecting multiple files contain in a single file:
cat test test1 > test2
- sorting contents of multiple files in a single file:
cat test test1 | sort > test2
find / -name myfile.txt
- first part: find
- second part: where to start search from,
/
means the whole drive - third part: an expression which determines what to find
- fourth part: the name of the thing to find
- Find all files within current folder that accessed more than 100 days ago:
find . -atime 100
- Find all empty files and folders in your system:
find / -empty
- Find all executable files:
find / -exec
- Find all readable files:
find / -read
- Find all files with extension .mps:
find / -name *.mp3
- To avoid matching files 'bat' in the middle like embattled.c, you could use:
find /path/to/folder -name '*bat.c' -o -name 'bat*.c'
-o
is the or operator.
- If you want to search case-insensitively, so files containing BAT, bAt, and so forth are matched, use the
-iname
test instead of the-name
test:find /path/to/folder -iname '*bat*'
- If you want to find both regular files and symbolic links, you can use:
find /path/to/folder -name '*bat*' \( -type f -o -type l \)
- Display found file/folder details
find /path/to/folder -name '*bat*' -ls
- Send output of find to a file
find /path/to/folder -name '*bat*' -fprint FileToPrintTo
- Search for and edit a file at the same time:
find . -name a.c -exec nano '{}' \;
processes text line by line and prints any lines which match a specified PATTERN, Matches a regular expression against text in a file, multiple files, or a stream of input.
- Find the lines containing
keyword
:grep "keyword" yourfile
- Highlight the
keyword
with color and line number:grep --color -n "keyword" yourfile
-i
to ignore case-r
to recursively search subdirectories-c
to print a count of matching lines-o
to print only the matching parts of a matching line-v
==--invert-math
to select non-matching lines
- Replace
yourfile
with*
to match any file or folder
- For BSD or GNU
grep
you can use-B num
to set how many lines before the match and-A num
for the number of lines after the match.grep -B 3 -A 2 foo README.txt
- If you want the same number of lines before and after you can use
-C num
.grep -C 3 foo README.txt
- This will show 3 lines before and 3 lines after.
- To grep special symbols like
>
useegrep
.
single-character wildcard?
The question mark indicates zero or one occurrences of the preceding element. For example, colou?r matches both "color" and "colour".*
The asterisk indicates zero or more occurrences of the preceding element. For example, ab*c matches "ac", "abc", "abbc", "abbbc", and so on.+
The plus sign indicates one or more occurrences of the preceding element. For example, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac"..*
any number of any character.{n}
match exactly n times.{n,}
match n or more times.{n, m}
match at least n times, but not more than m times.
rgrep
: same asgrep -r
egrep
: same asgrep -E
, evaluates your PATTERN string as an entended regular expression.fgrep
: same asgrep -F
, evaluates your PATTERN string as a fixed string, every character is treated literally.
docker ps
- show running containers, use
-a
to show all.
- show running containers, use
docker images
- show all images
docker run --name new_container_name -p host_port:container_port existing_image_name
- run a new container with existing image
docker stop container_name
- stop the container
docker system prune -a
remove any stopped images and all unused images.docker kill $(docker ps -q)
kill all running images
- ctrl + command + k
- 删除线
gcc -g myprogram.c
compiles myprogram.c with debugging informationgdb a.out
open gdb with file a.out, but it does not run the program.- To run the program
r
r arg1 arg2
r < file1
running by feeding a file
- List lines of code
l
list 10 lines of source code around current linel 50
list 10 lines of source code around 50th linel myfunction
show myfunction
next
run program until next line, then pause. If the current line is a function, execute the entire function, then pause.step
run the next instruction, not line. If the current instructions is setting a variable, it is the same asnext
. If it is a function, it will jump into the function, execute the first statement, then pause.finish
finish executing the current function, then pause (also called step out). Useful if you accidentally stepped into a function.info local
show local variables
- Set a breakpoint, the program will pause when it reaches the breakpoint
break 45
set a breakpoint at line 45break myfunction
set a breakpoint at myfunction
watch x == 3
watchpoint which pauses the program when a condition changes (whenx == 3
changes).continue
resume execution after being paused by a breakpoint/watchpoint. The program will continue it hits the next breakpoint/watchpoint.delete N
delete breakpoint Ndisable
disable all breakpoints- To list current breakpoints:
info break
- To delete a breakpoint:
del [breakpointnumber]
- To temporarily disable a breakpoint:
dis [breakpointnumber]
- To enable a breakpoint:
en [breakpointnumber]
print x
print current value ofx
set x = 3
orset x = y
setx
to value3
or another variabley
call myfunction()
orcall strlen(mystring)
call user-defined or system functions.display x
undisplay x
constantly display value of variable x which is shown after every step or pause.
bt
backtrace, print the current function stack to show where you are in the current program.bt full
backtrace including local varablesup
down
move to the next frame up or down in the function stack.return
return from current function.
gdb myprogram core
debug myprogram with "core" as the core dump filebt
print the backtrace at the point of the crash. Examine variables using the techniques above.
handle [signalname] [action]
handle SIGUSR1 nostop
handle SIGUSR1 noprint
handle SIGUSR1 ignore
- git clone depth=1 your_git_repo
- only clone current version, no history
git add -v -u
verbose and only add updated files not new files- After
git fetch
,git log
won't show new commits onorigin/master
, you need to usegit log origin/master
to show commits from both localmaster
andorigin/master
. - To reset some file that has been added to stage,
git reset file_name
- To abandon changes in one file that has not been added to stage,
git checkout file_name
- You can use git checkout to check a single file out of the stash:
git checkout stash@{0} -- <filename>
- Rebase
- Suppose you are working on a branch
feature
and want to rebasefeature
on latestmaster
- You are on
feature
and dogit rebase master
- You get conflict since both of the branches have modified same file
test.txt
- To ignore every modification from
master
and usefeature
branch, you dogit checkout --theirs test.txt
because you are rebasing on top ofmaster
,master
isours
. - Note, when you are doing
merge
,ours
andtheirs
are opposite from rebase.
- Suppose you are working on a branch
git diff
can show you the difference between two commits:git diff mybranch master -- myfile.cs
- Push local
branch1
to remotebranch2
git push origin branch1:branch2
- Push remote
branch1
to a new remotebranch2
git co origin/branch1
git co -b branch2
git push --set-upstream origin branch2
git push
- to indent a block of lines
- V enter visual mode, press j select lines you want to indent, press >
- y to copy
- 0 moves the cursor to the beginning of the line.
- $ moves the cursor to the end of the line.
- gg move to the beginning of the file.
- G move to the end of the file
d
delete and stay in command mode,c
cut and stay in insert mode,y
yank, following operations work for all left commands.dl
delete a letterdw
delete a worddd
delete a line5dd
delete 5 lines
p
paste after cursor,P
paste before cursor
Provides a number of debugging and profiling tools that help you make your program faster and more correct. The most popular of these tools is called Memcheck, which can detect many memory-related errors that are common in C and C++ programs and that can lead to crashes and unpredicted behaviours.
- check possible memory leak for C/C++ programs:
valgrind --leak-check=yes myprog arg1 arg2