Terminal Commands
5 min readApr 12, 2018
Popular commands brief for a fast access
FLAGS
* match the pattern
~ telda
| pipe
r recursivly
w write
f force
i bang
v print log
Terminal tips
tab hit to auto completereset resets all terminal default look & othersclear clean terminal window & you can scroll for old writings
Ctrl+L clean terminal window & you can scroll for old writings!x repeats the most recent command starting with xhistory grape all the commands you wrote entirely the history of the bash shellman X show you the manual of command ”X”echo “word” print “word” on the screen
echo “word” > name put “word” in a new file called name
echo “word” >> name adds “word” + previous words into file called name./mario < inputFile takes data from inputFile to use it as input into mario filehistory | grep ls takes all the history outputs & feeding it to grep command so that grep recognizes the ls words in itcat fileName looks inside that filename, Prints current file contents to stdout; semi colon allow us to run many programs in same line, Just divide between them by a semi colonwhich commandName check if you have a program installed that uses that commandName
COMMANDS
Files & Directories
- list
ls list the folders & files in this directory
ls -l list files + more info + permissions
ls -a list the folders + Hidden
ls -la list the folders + Hidden + Permissions
- cd change directory
cd location change directory
cd ~/location go back till this location directory
cd .. up one level location
cd . takes me to the current Directory
cd - takes me to the last Directory I was in before the current
cd ../directory1/directory2/ takes you to directory2 that's inside directory1pwf show you the current directory location details
- cut & copy , create files & directories
mv fileName newFilename rename file
mv anyFile anyFolder anyFile lastFolder moves everything in the lastFoldercp oldFile.c newFile.c copy the oldFile & name the newFiletouch filename create a new emty filename
mkdir Name makes a new directory(folder) called Name
rmdir Name remove the directory if only it's empty
- delete, remove files & directories
rm file.c remove the file with ask you first (only files)
rm -f file.c -f flag forces removing file without Asking
rm -r Name remove the directory
rm -rf Name remove the directory & all it's files + f without Askingrm *1 remove the files & directories with pattern 1 in it's name
rm * remove all the contents of the current directory
rm -i -i flag requires user’s confirmationDANGEROUS: don't do it
rm -rf ~ removes everything in home directory
rm -rf / removes everything in root directory
search & find words, files, directories
- grep
grep “wanted word” recognizes wanted word while typing
grep “wanted word” X search for “wanted word” in X Location, files
grep “wanted word” * search for “wanted word” in all files &foldersgrep -r search in directories & sub-directory
- find
find . -name “cat” search for folder or file with the exact name “cat”find . -name “*cat” search for folder or file with after pattern name “cat”find . -name “cat*” search for folder or file with after pattern name “cat”find . -name *.c search for folder or file with .c extenction
find . -type d search for all directories in the current directoryfind . -type f search for all files in the current directory
Text Editors
- gedit & nano
gedit opens gedit & you can't use Terminal till you close Gedit
gedit name.txt opens gedit & make file called namenano opens a bacic text Terminal Editor
nano filename opens filename in nano texteditor
ALT+L turn off text-wrapping
- vim, vim commands
vim filename opens filename in cim rich texteditor
vi filename opens filename in cim rich texteditor
*PIPES*
- redirect.c
int main()
{
string input = GetString();
printf("%s\n", input);
}
- stdout standard output
./redirect > file.txt
saves the output to the file.txt & replace evetthing in file.txt./redirect >> file.txt
Append (Adds) the output to the current contents in file.txt2>
prints only error messages./redirect > /dev/null
output will be discarded, Data Black hole
- stdin standard input
./redirect < file.txt > file2.txt
Get the input from file.txt & saves the output to the file2.txt & replace everything in file2.txtNotice
We only have 1 GetString in redirect.c so it will take only 1 line input
Output of a program to Input of another program
- hello.c
int main()
{
printf("Hello World\n");
}
- command
./hello > input.txt
Take hello output into input.txt./redirect < input.txt
Take input.txt as an Input./hello > input.txt; ./redirect <input.txt
same result of the first two just using one line
Output of a program on the “Pipe Left” to Input of another program on “Pipe Right”
- pipeline
To avoid creating that extra file "input.txt" from previous example./hello | ./redirect
same result as previous just using pipe
Combine Command lines using Pipeline
wc -l
-l flag to count lines, wc to countls | wc -l
combining ls command as stout to show the current files in directory,
then use this result as stdin to ws -l to count the words,
So we know how many files in this directorycat students.txt | sort
Will get the stdin from cat and stdout to sort, result show them in alphabetical ordercat students.txt | sort | uniq > final.txt
Saves all sorted without duplication names in final.txtsort < students.txt | uniq > final2.txt
same result
Extras
operators
| or 0|0= 0, 0|1 = 1
^ exclusively different or 0^0= 0, 0^1 = 1
~ telda flipping operator from 0 to 1, 1 to 0
& And 0&0 = 0 , 1&0 = 0
<< : shift operator 1<<7 = 10000000
echo
echo $?
Echoes (prints) the exit value for the previous command.
If it failed it will be different than zero (0).
$ cd ~
$ echo $?
> 0$ cd //whatever/
> bash: cd: //whatever/: No such file or directory
$ echo $?
> 1
make file, More details
clang file.c
compile the file in c language, clang is a very basic c language compilerclang -ggdb3 -O0 -std=c99 -Wall -Werror soso.c -lm -o soso
full clang = makemake file
compile the file with all the clang flags