Terminal
#ProTip It's about using || and && as shell piping tools: command1 || command 2 # command 2 will only run if command 2 has failed (non-zero return) command1 && command 2 # command 2 will only run if command 2 has NOT failed (zero return) torrent: deluge cp -rf a/* b/ if this doesn't work it is because cp is aliased to something else with some flags set. unalias a command for the session with prefixing it with \ e.g. \cp OR use $ yes | cp -rf a/* b/ systemctl --> handles system services ubuntu disable fancy view theme
ultimate ubuntu speed up in virtualbox link Execute the following command to see if 3D acceleration is being used or not:$ /usr/lib/nux/unity_support_test -pIt will probably say: Now that’s bad news, because the graphical interface of Ubuntu makes your whole system slow and laggy. Once this is installed, we now install the vboxvideo driver: $ sudo bash -c 'echo vboxvideo >> /etc/modules' Now, shutdown Ubuntu. Then, you open the settings of your virtual Ubuntu and you go to ‘Display‘. Now tick ‘Enable 3D Acceleration‘. Move faster in bash
shuffle a text file line by line:
for i in $(seq 1 10); do time curl -s https://get.fabric.io > /dev/null; done 2>&1 | grep real copy as curl.
jq commandline parsing of json. $ brew install jq https://jqplay.org/ Ctrl-r keep pressing Ctrlr to see previous matches $ cat * | grep -C5 exception # also print 5 lines before and after grep -v 'RuntimeException' select linesthat dont match perf-top(1): System profiling tool - Linux man page$ comm -23 file1 file2 # get diff of llines that are in file1 vs file 2 $ comm -23 file2 file1 if you have a separate drive for /boot and it is full: make a new directory soemwhere and move a few of the kernel files of hte same version to that folder. BUT keep the last two versions. link grep 'create' -ir --exclude-dir={\*node_modules\*,\*migrations\*} . mkdir t && cd $_
http://www.brianstorti.com/stop-using-tail/ Use zgrep to grep a gzip (gz) file http://alvinalexander.com/blog/post/linux-unix/how-grep-search-compressed-gzip-gz-text-fileTurn off Monitor from command line $ xset dpms force off and to turn the monitor back on:$ xset dpms force on $ xset -q batch rename: $ rename .src .c * # renames all files in current directory with extension .src to .c
To pause a process: Ctrl-z on the terminal it is running to resume: $ fg $ file a.txt # determine file type $ time sleep 5 # time track how much time a process takes to finish watch " ls -lSh /media/sde/entitySIs" watch "grep '^>' run*Log.txt" # to check if a new line with > in the beginning has been found in the text files in this directorydaemon tools : toolset for UNIX services. supervise: monitors a service. It starts the service and restarts the service if it dies. softlimit runs another program with new resource limits. printenv print environemnt variables
Who is online? $ w $ uptime What Linux version are you running? cat /proc/version ; cat /etc/*release* ; uname -a ; whoami ; hostname ; find /etc/ -type f -maxdepth 1 -name "*release*" 2> /dev/null | xargs cat Everything on the system that produces or accepts data is treated as a file; this includes hardware devices like disk drives and terminals. man: The man command is used to show you the manual of other commands. Try "man man" to get the man page for man itself. e.g. man nautilus info --> lists system commands and a brief description of what they do.
CTRL + L ---> for location bar
Split view to browse to two differnet locations at the same time. View->Extra Pane Use tabs Ctrl-T Desktops/Workspace Switch desktop Ctrl-Alt ArrowKeys
Move windows between dektops/workspaces Shift-Ctrl-Alt ArrowKeys Lock Screen Ctrl-Alt L Show Desktop Ctrl-Alt-D OR SUPER-D Open home folder SUPER-Num1 su logs in as root user for all commands that follows, use exit to log out of su
sudo just runs for one command as current user
$ gksudo # run graphical applications using this 'You should never use normal sudo to start graphical applications as Root. You should use gksudo"
Reboot/shutdown
Daily Works (bash commands)
/ means filesystem root (/home --> the / specifies the filesystem root) |
Terminal Profile: Screen size : 105 * 33 |
Clear Terminal: $ reset
ls /usr*/[be]* : any file in directories starting with usr and the file name begins with b or e Brace Expansion b{ar{d,n,k},ed}s --> This will result in the expansion bards, barns, barks, and beds |
||||||||||
Multiple Commands per line $ cd docs; mkdir old_docs; mv *.* old_docs is the same as $ cd docs $ mkdir old_docs $ mv *.* old_docs |
Keyboard Special Signals Processes Ctrl-c = ^C (default action: terminate the process) SIGINT
Ctrl r search prev typed commandsCtrl-d = ^\ (default action: terminate the process and dump to a core file) SIGQUIT Ctrl-z = ^Z (default action: suspend the process) SIGSUSP Shift-PageUp see upper screens of terminal Note: These are overridable by the running process |
||||||||||
background process (non-blocking) Run a command in background and get the input back to terminal immediately: $ command& follow the command name with an ampersand(&) like $ uncompress gcc.tar & to check for background jobs : $ jobs will print running jobs |
|||||||||||
$ history $ more : page by page output scrolling $ history | more history: you can also have a look at word designators and how to access arguments to former commands
|
I/O Redirection standard input / standard output / standard error
program 1 > sys_out_file_path 2 > sys_err_file_path
ls -Ila | grep 'm' --> (same as dir) show all files(-a) and their details(-l) that contain 'm' in their names. (-I) ignore case
$ ls -lSrh # Sort by size, human readble. ls -lShls -ltra --> show all files(-a) sort by modification time (t) reverse the order of sort (r) and their details(-l)
$ du -ah --> recursive file size
$ tree -sh
cp index.html i.php --> copy 'index.html' to the current directory with new name 'i.php'.
cp -v: verbose(explain what's going on)
mkdir -p work/junk/questions make parent directory if not available
rm file.extention --> remove file
rm -r directory --> remove directory
rm -f --> no more confirmation
rm /path/*
rm
`
find /path -type f
`
find /path -type f -print0 | xargs -0 rm
find feeds the input of xargs with a long list of file names. xargs then splits this list into sublists and calls rm once for every sublist.
find /path -type f -exec rm '{}' \;
calls rm once for every single file.
find /path -type f -exec rm '{}' +
same as xargs version
These commands will not work as expected if there are whitespace characters, including newlines, in the filenames. In order to avoid this limitation one may use: find . -name "*.foo" -print0 | xargs -0 grep bar
You can also use -L to limit the number of arguments. If you do that, the command will be run repeatedly until it is out of arguments. Thus, -L1 runs the command once for each argument (needed for tools like tar and such).
$ find . -name "*.bak" -print0 | xargs -0 -I {} mv {} ~/old.files ---> -I assigns a name to the input list and makes it possible to locate the exact position that the input list should be put in the command xargs will invoke.
Search File (Content)
-r recursively search all files under each directory
-l
-q quiet: don't print the line you found it
grep -v negative matching:
-v, --invert-match select non-matching lines
-iname ignore case
-type d | f | s (dir, file, socket)
-exec command {} \;
-or
-not (e.g. -not -name 'myFile*')
-ls : Print the names, dates, sizes, and so on of matching files.
-size +10000k
<<-exec in find forks a new process for each call, whereas xargs appends file names to one and forks one process to work on the file list.>>
$ find . -type f -exec ls -s {} \; | sort -n | head -5 # 5 largest files in a directory
find `pwd` -name .htaccess find with absolute names
find /home/username/xhtml_repos -name '*.xhtml' -print | xargs perl xmlpp.pl -z //-print shows which file is being processed
find . -name '*.htslp' -exec grep -l 'mortez' {} \; --> find all files with name ending in htslp where the text 'mortez' is inside it
find . -exec grep -l 'mortez' {} \;
find / -name '*was*' 2>/dev/null
$ find /mp3-collection -size +10000k ! -name "Metallica*"
$ find /mp3-collection -name 'Metallica*' -or -size +10000k
$ rmao
* Alternatively you could use 2>error.txt where after the search is completed you would have a file named error.txt in the current directory with all the error messages in it.
find /home/username/xhtml_repos -name '*.xhtml' -print | xargs perl xmlpp.pl -z //-print shows which file is being processed
do
echo "Listing coincidences inside ${file}:"
unzip -l "$file" | grep "fileName"
done
pwd -P get current directory, ignoring symbolic links
zip
$ tar -cf project1.tar proje
ct1
_directory
$ gzip project1.tar
//make tar.gz
tar -zcvf archive-name.tar.gz directory-name -z: Compress archive using gzip program, -c: Create archive, -v: Verbose i.e display progress while creating archive, -f: Archive File name
$ unzip -l zipFileName //list content of zip file or jar file, etc
$ unzip \*.zip
$ tar zxf file.tar.gz -C directory
$ tar zxf file.tgz
$ tar jxf file.tar.bz2
$ tar jxf file.tbz2
for i in *.tar.gz; do tar xvzf $i -C path/to/output/directory; done # to extract all zip files to directory
To unzip and rezip:
rm zipFile.zip
zip temp/* zipFile.zip
rm -r temp/
Batch Rename
$ rename s/"SEARCH"/"REPLACE"/g *
Run/Install Programs
sudo apt-get install phpmyadmin --> install software called phpmyadmin
sudo apt-get remove phpmyadmin
sudo apt-get autoremove To remove packages that were automatically installed to satisfy dependencies for some package and that are no more needed.
sudo chown -R morteza (OR $USER) . --> Own a directory so that it doesn't ask for password all the time to do any mere change
To Execute
To make a file have executable attributes
Less Common Commands
$ ls -ldrwxrwxrwx 1 username users 2525 Feb 18 09:17 index.htm
^\ /\ /\ / ^ \ / \ /
| V V V | ''|''' '|'
| | | | | | `-- group the file belongs to
| | | | | `-- user who owns the file
| | | | `--the number of links (directory entries that refer to the file)
| | | `-- others (users who are neither you or in the group [WORLD!])
| | `-- group (people in the group)
| `-- user that owns the file
|
`-- d=directory, -=file, l=link, etc
User: The username of the person who owns the file. By default, the user who creates the file will become its owner.
Group: The usergroup that owns the file. All users who belong into the group that owns the file will have the same access permissions to the file. This is useful if, for example, you have a project that requires a bunch of different users to be able to access certain files, while others can't. In that case, you'll add all the users into the same group, make sure the required files are owned by that group, and set the file's group permissions accordingly.
Other: A user who isn't the owner of the file and doesn't belong in the same group the file does. In other words, if you set a permission for the "other" category, it will affect everyone else by default. For this reason, people often talk about setting the "world" permission bit when they mean setting the permissions for "other."
chmod u+wx testfile chmod ug-x testfile chmod a=r testfile |
chmod 640 testfile chmod 755 testfile ::: You read write execute, group and others read and execute chmod 600 testfile only you can rw, others can't even see it To make sure that group ownership is inherited on future files: chmod -R g+s /tmp/mytemp* |
$ ssh user@host.domain.etc OR user@ip_address
$ sudo tcpdump -nne to view mac addresses and port numbers
finger
$ finger reports all users of local machine
$ finger username all details of the user
$ finger @computerName all users of specified host
$ which firefox
$ htop similar to top more interactive $ glances
$ htop -d 100 # update htop every 10 seconds
$ iotop table of processes for disk io measurements$ dstat '' '' '' '' ' ' ' '' ' ' '
$ ps aux | grep httpd
kill -9 pid kill process with pid no.e
pkill -9 -f .*Pi.*
Memory - Disk space
$ df -h available disk space
$ du -sh directory size
$ gparted # format and partition
lsof list open files lsof -Pnl +M -i4 list ip4 ports
---------------------------------------------------------------------------------------------------------------------------------------------------------
Linux Pipeline Execution
$ cat fred barney | sort | ./your_program | grep something | lprThis line says that the cat command should print out all of the lines of file fred followed by all of the lines of file barney. Then that output should be the input of the sort command, which sorts those lines and passes them on to your_program. After it has done its processing, your_program will send the data on to grep, which discards certain lines in the data, sending the others on to the lpr command, which should print everything that it gets on a printer. Whew!
Pipelines like that are common in Unix and many other systems today because they let you build powerful, complex commands out of simple, standard building blocks. Each building block does one thing very well, and it’s your job to use them together in the right way.
There’s one more standard I/O stream. If (in the previous example) your_program had to emit any warnings or other diagnostic messages, those shouldn’t go down the pipeline. The grep command is set to discard anything that it hasn’t specifically been told to look for, and so it will most likely discard the warnings. Even if it did keep the warnings, you probably don’t want to pass them downstream to the other programs in the pipeline. So that’s why there’s also the standard error stream: STDERR. Even if the
standard output is going to another program or file, the errors will go to wherever the user desires. By default, the errors will generally go to the user’s display screen,* but the user may send the errors to a file with a shell command like this one:
Also, generally, errors aren’t buffered. That means that if the standard error and standard output streams are both going to the same place (such as the monitor), the errors may appear earlier than the normal output. For example, if your program prints a line of ordinary text, then tries to divide by zero, the output may show the message about dividing by zero first, and the ordinary text second.
If you want to save the output of program1
into a file and pipe it into program2
, you can use tee(1)
:
program1 arg arg | tee output-file | program2 arg arg
to allow the second program to process data as it comes out from the first program, before the first program has completed its operation.
System Parameters
"Configuration Editor - Directly edit your entire configuration database. - Linux registry" The Configuration Editor is often referred to as "GConf".
GConf provides a central storage location for preferences, ---/apps/gnome-system-tools/users
Standard Input (Console) as File Parameter
Linux Important
/usr/lib : contains shared objects of the libraries, which can be used by other programs but not as for development/usr/include : contains the header files of the libraries you might use to code and make and make install other packages.for example for libxml2 I installed it and it was available but when I wanted to install the xmllib which required xmllib2 it couldn't find it, and the error message said that you might need the -devel .h header file. when we checked in the /usr/include it didn't have xmllib2. then we installed the xmllib2-dev via sudo apt-get install xmllib2-dev which includes the header files, then xmllib recognized the xmllib2.
Files
/var/log/syslog :/var/log/auth.log/var/log/kern.log/etc/environment : system environment variables/home/username/.local/share/Trash or /root/.local/share/Trash
$ ip route
OR
$ cat /etc/resolv.conf
Renew IP:
OR set manually $ ifconfing ethX inet xxx.xxx.xxx.xxx
OR $ sudo dhclient
OR $ sudo /etc/init.d/networking restart
Sharing
Check internet connectivity
OR $ wget www.google.com to download a whole webpage
OR for text-based browsing install and use lynx
Name an IP if you don't have a name associated with an ip, you can do it internally by modifying the following file:
Firefox initial home page: chrome://ubufox/content/startpage.html
active ports and network connections
$ netstat -an | grep "LISTEN "
Modify Resolution (Virtual Machine)!
install system-config-display (sudo apt-get system-install or yum install system-config-display)
run it and modify the hardware monitor to the resolution you wish
Symbolic Link
To mount windows file system
Just open up terminal and write
sudo fdisk -l
. then try figuring out which partition is your c drive. and then just mount the partition as:sudo mkdir /storage sudo mount /dev/sda3 /storage //in case dev3 is your c drive.
Why not enable mount of the windows partition on boot time? There is a utility called ntfs-config which mounts your windows partition on boot time. install it as
sudo apt-get install ntfs-config
and enable mount at boot as
gksudo
Create an Application Shortcut to Open Nautilus as Root in Ubuntu
[Desktop Entry] Name=File Browser (Root) Comment=Browse the filesystem with the file manager Exec=gksudo nautilus Icon=file-manager Terminal=false Type=Application Categories=Application;System; |
Y
Press Enter
Drag 'n Drop as Sudo
Automate or schedule Tasks (cron)
* * * * * /bin/execute/this/script.sh
2. hour (from 0 to 23)
3. day of month (from 1 to 31)
4. month (from 1 to 12)
5. day of week (from 0 to 6) (0=Sunday)
0 1 * * 1-5 /bin/execute/this/script.sh
@yearly Run once a year "0 0 1 1 *"
@annually (same as @yearly)
@monthly Run once a month "0 0 1 * *"
@weekly Run once a week "0 0 * * 0"
@daily Run once a day "0 0 * * *"
@midnight (same as @daily)
@hourly Run once an hour "0 * * * *
installrpm redhat rpm -ihv paketname.rpm
You can also use nc
(NetCat) to transfer the data. On the receiving machine (e.g., host.example.com):
nc -l 1234 > big.txt
This will set up nc
to listen to port 1234 and copy anything sent to that port to the big.txt
file. Then, on the sending machine:
echo "Lots of data" | nc host.example.com 1234
This command will tell nc
on the sending side to connect to port 1234 on the receiver and copy the data from stdin across the network.
However, the nc
solution has a few downsides:
- There's no authentication; anyone could connect to port 1234 and send data to the file.
- The data is not encrypted, as it would be with
ssh
.
===============================
$ lvmdiskscan
$ mkfs.ext4 /dev/sdf
$ mkdir /mnt/sdf
$ mount -t ext4 /dev/sdf/ /mnt/sdf/
Convert MP# to WAV to be burnt in an AUDIO CD
do
mpg123 -w ./"${file}".wav "$file"
done
Parallel execution over large file
time find /media/sd{d,e}/s3.amazonaws.com/ -name '*.gpg'\ | parallel -u -j+0 --progress "gpg --quiet --no-permission-warning --trust-model always --decrypt {} | xz --decompress --stdout | ../cpp/entity_match '{}'"