This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
linuxfind [2019/07/18 04:36] external edit
linuxfind [2021/05/12 22:11]
Line 1: Line 1:
-====== Linux Find Command ====== +====== Linux Cheat Sheet ====== 
- +The contents of this file are released under the GNU General Public License. Feel free to reuse the contents of this work, as long as the resultant works give proper attribution and are made publicly available under the GNU General Public License. 
- +===== Basics ===== 
-===== Find empty directories ===== +==== Getting help ==== 
-<file bash>find $HOME -depth -type d -empty</file> +View the manual for target command  
-This will find empty directories in your home directory. +<file bash>man command</file> 
- +Get help with a target command (probably the same as above, but not always):  
-===== Find empty files =====  +<file bash>command -h</file> 
-<file bash>find $HOME -depth -type f -empty</file> +In case you forget the name of a command, print possible commands relating to any given word:  
-This will finde empty common files in your home directory. +<file bash>apropos word</file> 
- +View index of help pages: 
-===== Find a file with specific name =====  +<file bash>info</file> 
-<file bash>find $HOME -name [name_of_file]</file> +==== Command Line Utilities ==== 
-This will find files with a given name in any child directory of your home+=== Basic File and Directory Operations === 
- +Print current working directory:  
-===== Find a file with a specific extension ===== +<file bash>pwd</file> 
-<file bash>find $HOME -name "*.[given_extension]"</file> +Show files in current directory:  
-This find files wit the given extension all along your home, an example to find jpg files is+<file bash>ls</file> 
-<file bash>find $HOME -name '*.jpg'</file> +Show maximum information about all files, including hidden:  
- +<file bash>ls -a</file> 
-===== Find files with specific permissions ===== +Recurse into subdirectories and list those as well:  
-<file bash>find $HOME -perm [permission bits]</file> +<file bash>ls -R</file> 
-This will find files with the given permission bits in your homeas an example we can look for .txt files that can have 644 bits on+List files by modification time, most recent first.  
-<file bash>find $HOME -name '*.txt' -perm 644</file> +<file bash>ls -lt</file> 
- +Move/rename a file or directory (be careful that you don't move the source over destination with the same name):  
-===== Find files with some given permissions ===== +<file bash>mv source destination</file> 
-<file bash>find $HOME -perm -[permision_bits]</file> +Delete target forever (be very careful), use -r recursive flag for directories:  
-This will find files in your home that have match with the given permissions but that can also have some others, as an example+<file bash>rm target</file> 
-<file bash>find $HOME -name '*.txt' -perm -644</file> +Copy file or directory:  
-This will find the files with 644 but also some with 664 or 777 or anything "greater" than 644.  +<file bash>cp source destination</file> 
- +Mount filesytem:  
-Output comparison Let's see some output comparison for this to be better understood. +<file bash>mount /dev/device/name /media/device/name</file> 
- +Unmount:  
-<file bash> +<file bash>umount /media/device_name</file> 
-find $HOME -name '*.txt' -perm 644 -exec ls -l {} \; +Forensically clone filesystems and do other low-level operations on files. Be careful with this oneCan be destructive:  
--rw-r--r-- 1 root root 181 2007-10-09 23:27 /home/benp/a.txt +<file bash>dd</file> 
--rw-r--r-- 1 benp benp 2757 2007-08-29 23:52 /home/benp/b.txt +Work with disk partitions:  
--rw-r--r-- 1 benp benp 77431 2007-09-05 23:11 /home/benp/c.txt+<file bash>parted</file> 
 +Filesystem creation tool:  
 +<file bash>mkfs</file> 
 +=== System Administration ==
 +Execute command as an administrator (can be destructive/insecure. Use only for system administration tasks):  
 +<file bash>sudo command</file> 
 +Become system administrator:  
 +<file bash>sudo -s</file> 
 +Quit system administration:  
 +<file bash>exit</file> 
 +Forgot to type sudo in front of a command and already hit enter? Repeat the last command using sudo:  
 +<file bash>sudo !!</file> 
 +=== Installing software from a .tgz (also known as a tarball) === 
 +First, unzip the tarball (see section on tar, below)  
 +Next, move into unzipped directory 
 +<file bash>cd software_directory</file> 
 +Always read README first if it is providedin case there are any modifications to the procedure outlined below:  
 +<file bash>cat README</file> 
 +Automatically check for appropriate configurations and generate a MAKE file in the directory:  
 +<file bash>./configure</file> 
 +Compile softwareMay require sudo:  
 +<file bash>make</file> 
 +Move files into their appropriate locationsMay also require sudo:  
 +<file bash>make install</file> 
 +Clean up files in directory, in case make command fails, or just to remove unnecessary cruft:  
 +<file bash>make clean</file> 
 +=== Ubuntu/Debian Software repositories === 
 +Check distro repositories for software updates:  
 +<file bash>sudo apt-get update</file> 
 +Download and install updates (update first):  
 +<file bash>sudo apt-get upgrade</file> 
 +Search for package in the repositories:  
 +<file bash>apt-cache search keyword</file> 
 +Get more detail on one specific package:  
 +<file bash>apt-cache show package_name</file> 
 +Download and install a package:  
 +<file bash>sudo apt-get install package_name</file> 
 +View the output of a command in a more convenient format:  
 +<file bash>command | less</file> 
 +=== Working With Files === 
 +Print a file in terminal:  
 +<file bash>cat file</file
 +Find files matching filename 
 +<file bash>locate filename</file> 
 +See the version of program or the location of the program  
 +<file bash>which appname</file> 
 +Search through filename for matches to phrase 
 +<file bash>grep phrase filename</file> 
 +Search through output of a command for phrase:  
 +<file bash>command | grep phrase
 </file> </file>
- +=== Working With Processes === 
-<file bash> +List all running processes:  
-find $HOME -name '*.txt' -perm -644 -exec ls -l {} \; +<file bash>ps -e</file> 
--rw-r--r-- 1 root root 181 2007-10-09 23:27 /home/benp/a.txt +Standard system monitor showing a more extensive view of all processes and system resources 
--rw-r--r-- 1 benp benp 2757 2007-08-29 23:52 /home/benp/b.txt +<file bash>top</file> 
--rw-r--r-- 1 benp benp 77431 2007-09-05 23:11 /home/benp/c.txt +Like top, but with a better, cleaner interface 
--rw-rw-r-- 1 benp benp 464 2007-09-06 01:23 /home/benp/d.txt+<file bash>htop</file> 
 +Stop a process from using all system resources and lagging computer 
 +<file bash>renice process_name</file> 
 +Kill misbehaving process (use sparingly, last resort, try 'renice' command first) 
 +<file bash>pkill process name</file> 
 +Start a process in the background  
 +<file bash>command &</file> 
 +Start a process in the background and have it keep running after you log off  
 +<file bash>nohup command &
 </file> </file>
- +=== Compression and Encryption === 
-As you may see in the first example we do not see the file <file bash>d.txt</file> as it has 664 permissions. +Make a simple compressed backup of files or directories:  
- +<file bash>tar -cvzf backup/output.tgz target_files_or_directories</file> 
-But, on the second output, it is listed as it has "greater" permissions than 644. +Open a compressed .tgz or .tar.gz file:  
- +<file bash>tar -xvf target.tgz</file> 
-===== Find files of given sizes =====  +Encrypt file:  
-<file bash>find -size n[cwbkMG]</file> +<file bash>gpg -o outputfilename.gpg -c target_file</file> 
-This will output the files of given block size, as an example we can see: +Decrypt a file:  
- +<file bash>gpg -o outputfilename -d target.gpg</file> 
-<file bash>find $HOME -name '*.txt' -size 4k -exec ls -l {} \;</file> +Zip and encrypt a directory simultaneously:  
-And the output is: +<file bash>gpg-zip -o encrypted_filename.tgz.gpg --s file_to_be_encrypted</file> 
- +==== The Bash shell ==== 
-<file bash> +=== File Name expansions === 
--rw-r--r-- 1 benpbenp 3707 2007-07-25 15:48 /home/benp/README.txt +Current user'home directory:  
--rw-r--r-- 1 benpbenp 4043 2007-01-22 15:44 /home/benp/Desktop/front/README.txt +<file bash>~/</file> 
--rw-r--r-- 1 benp benp 3112 2007-09-23 15:39 /home/benp/Desktop/adsense/README.txt +Current directory 
--rw-r--r-- 1 benp benp 3707 2007-09-23 15:39 /home/benp/Desktop/akismet/README.txt +<file bash>./</file> 
--rw-r--r-- 1 benp benp 3616 2007-09-23 15:39 /home/benp/Desktop/show-to-install.txt+Parent directory 
 +<file bash>../</file> 
 +Or even (Two parent directories down) 
 +<file bash>../../</file> 
 +All files in target directory(Be very careful.):  
 +<file bash>/*
 </file> </file>
- +=== Output Redirects === 
-Now let'see this other: +Redirect output of one command into the input of another with a pipe:  
- +<file bash>command_1 | command_2</file> 
-<file bash>find $HOME -name '*.txt' -size 5k -exec ls -l {} \;</file> +Or even:  
-And the output is+<file bash>command_1 | command_2 | command_3</file> 
- +Redirect output to a file:  
-<file bash> +<file bash>command > file</file> 
--rw-r--r-- 1 benp benp 4496 2007-09-23 15:39 /home/benp/captcha.txt+Or:  
 +<file bash>file > file</file> 
 +Or even, to redirect in a different direction:  
 +<file bash>file < file</file> 
 +Append output rather than writing over the target file:  
 +<file bash>file_or_command >> file</file> 
 +Works like |, but it writes output to both target and terminal:  
 +<file bash>tee target</file> 
 +Redirect standard output and error to /dev/null, where it is deleted.  
 +<file bash>command > /dev/null 2>&
 +=== Controlling Execution === 
 +Wait until command 1 is finished to execute command 2  
 +<file bash>command_1 ; command_2</file> 
 +Or even:  
 +<file bash>command_1 ; command_2 ; command_3</file> 
 +&& acts like ; but only executes command2 if command_1 indicates that it succeeded without error by returning 0.  
 +<file bash>command_1 && command_2</file> 
 +double pipes || act like && but only executes command_2 if command_1 indicates an error by returning 1.  
 +<file bash>command_1 || command_2 
 +=== Bash Wildcards === 
 +Zero or more characters:  
 +<file bash>  * </file> 
 +Matches "phrase" and any number of trailing characters:  
 +<file bash>phrase*</file> 
 +Matches any incidences of "phrase" with any trailing or leading chars:  
 +<file bash>*phrase*</file> 
 +Matches any one char:  
 +<file bash>?</file> 
 +Matches any of the characters listed inside brackets:  
 +<file bash>[chars]</file> 
 +Matches a range of chars between a-z:  
 +<file bash>[a-z]</file> 
 +===== Advanced ===== 
 +==== Command Line Utilities, Continued ==== 
 +=== Networking === 
 +Configure network interfaces:  
 +<file bash>ifconfig</file> 
 +Configure wireless network interfaces:  
 +<file bash>iwconfig</file> 
 +Connect to a remote server.  
 +<file bash>ssh username@ip_address</file> 
 +Forward X from target to current machine (Get a remote desktop. Somewhat obscure, but very useful):  
 +<file bash>ssh -X username@ip_address</file> 
 +Copy files/directory over the network from one machine to another recursively:  
 +<file bash>scp -r source_filename:username@ip_address target_filename:target_username@target_ip_address</file> 
 +Copy only changes between files or directories (super efficient way to sync directories, works either locally or with remote servers using username@ip_address:optionalport, just like ssh):  
 +<file bash>rsync source target</file> 
 +Check to see if target is online and responding  
 +<file bash>ping ip_address</file> 
 +View network route to target:  
 +<file bash>traceroute6 ip_address</file> 
 +Network Monitor  
 +<file bash>netstat</file> 
 +View firewall rules  
 +<file bash>iptables -L</file> 
 +Scan this machine(localhost) to check for open ports:  
 +<file bash>nmap localhost</file> 
 +== wget == 
 +download a file over http:  
 +<file bash>wget http://example.com/folder/file</file> 
 +complete a partially downloaded file:  
 +<file bash>wget -c http://example.com/folder/file</file> 
 +start download in background:  
 +<file bash>wget -b wget -c http://example.com/folder/file</file> 
 +download a file from ftp server:  
 +<file bash>wget --ftp-user=USER --ftp-password=PASS ftp://example.com/folder/file</file> 
 +== netcat == 
 +Listen for input from network on recieving_port, dump it to a file (insecure, but handy):  
 +<file bash>netcat -l recieving/port > file/copied</file> 
 +Pipe the output of a command to a target ip and port over the network:  
 +<file bash>command | netcat -w number_of_seconds_before_timeout target_ip target_port</file> 
 +Use tar to compress and output a file as a stream, pipe it to a target ip and port over the network:  
 +<file bash>sudo tar -czf - filename | netcat -w number_of_seconds_before_timeout target_ip target_port</file> 
 +=== Users and Groups === 
 +Change owner of a file or directory:  
 +<file bash>chown user_name:group_name directory_name</file> 
 +Change privileges over file or directory (see man page for details.)  
 +<file bash>chmod</file> 
 +Create a new user:  
 +<file bash>adduser</file> 
 +Change user privileges (be very careful with this one):  
 +<file bash>usermod</file> 
 +Delete user  
 +<file bash>deluser</file> 
 +Print groups:  
 +<file bash>groups</file> 
 +Create a new group:  
 +<file bash>groupadd</file> 
 +Change group privileges:  
 +<file bash>groupmod</file> 
 +Delete group:  
 +<file bash>delgroup</file> 
 +Temporarily become a different user:  
 +<file bash>su username</file> 
 +Print usernames of logged in users:  
 +<file bash>users</file> 
 +Write one line to another user from your terminal:  
 +<file bash>talk</file> 
 +Interactive talk program to talk to other users from terminal (must be installed from repositories.):  
 +<file bash>ytalk</file> 
 +=== Working With Files, Continued === 
 +View what processes are using what files:  
 +<file bash>lsof</file> 
 +View the differences between two files:  
 +<file bash>diff file_1 file_2</file> 
 +Output the top numberoflines of file:  
 +<file bash>head -n number_of_lines file</file> 
 +Like head, but it outputs the last -n lines:  
 +<file bash>tail -n number_of_lines file</file> 
 +Checksum a file:  
 +<file bash>md5sum file</file> 
 +Checksum every file in a directory (install this one from repositories.):  
 +<file bash>md5deep directory</file> 
 +Checksum a file (better algorithm with no hash collisions):  
 +<file bash>sha1sum</file> 
 +Same operation as md5deep, but using sha1:  
 +<file bash>sha1deep</file> 
 +Call command every few numberofseconds, and highlight difference in output:  
 +<file bash>watch -d -n number_of_seconds command</file> 
 +Execute command, print how long it took:  
 +<file bash>time command</file> 
 +View files in directory from largest to smallest:  
 +<file bash>du -a directory | sort -n -r | less</file> 
 +remove spaces from filenames in current directory:  
 +<file bash>rename -n 's/[\s]/''/g' *</file> 
 +change capitals to lowercase in filenames in current directory:  
 +<file bash>rename 'y/A-Z/a-z/' *</file> 
 +==== Environment and Hardware ==== 
 +print motherboard information  
 +<file bash>dmidecode</file> 
 +Print full date and time:  
 +<file bash>date</file> 
 +Print the hostname of this machine:  
 +<file bash>echo $HOSTNAME</file> 
 +Print information about current linux distro:  
 +<file bash>lsb_release -a</file> 
 +Or even:  
 +<file bash>more /etc/issue</file> 
 +Print linux kernel version:  
 +<file bash>uname -a</file> 
 +Print information about kernel modules:  
 +<file bash>lsmod</file> 
 +Configure kernel modules (never do this ;p ):  
 +<file bash>modprobe</file> 
 +View Installed packages:  
 +<file bash>dpkg --get-selections</file> 
 +Print environment variables:  
 +<file bash>printenv</file> 
 +List hardware connected via PCI ports:  
 +<file bash>lspci</file> 
 +List hardware connected via USB ports:  
 +<file bash>lsusb</file> 
 +Print hardware info stored in BIOS:  
 +<file bash>sudo dmidecode</file> 
 +Dump captured data off of wireless card:  
 +<file bash>dumpcap</file> 
 +Dump info about keyboard drivers:  
 +<file bash>dumpkeys</file> 
 +==== System Administration, Advanced (Continued) ==== 
 +Add a Personal Package Archive from Ubuntu Launchpad:  
 +<file bash>add-apt-repository</file> 
 +Install a .deb file from command line:  
 +<file bash>sudo dpkg -i package.deb 
 +==== Development ==== 
 +=== Python === 
 +Update pip (Python package manager):  
 +<file bash>pip install -U pip</file> 
 +search pip repos for a library:  
 +<file bash>pip search library_name</file> 
 +create a virtual python environment to allow install of many different versions of the same Python modules:  
 +<file bash>virtualenv dirname --no-site-packages</file> 
 +connect to a virtual python environment  
 +<file bash>source dirname/bin/activate</file
 +disconnect from a virtual python environment:  
 +<file bash>deactivate</file> 
 +install package into virtual python environment from outside:  
 +<file bash>pip install packagename==version_number -E dirname</file> 
 +export python virtual environment into a shareable format:  
 +<file bash>pip freeze -E dirname > requirements.txt</file> 
 +import python virtual environment from a requirements.txt file:  
 +<file bash>pip install -E dirname -r requirements.txt 
 +=== GIT === 
 +  All commands must be performed in the same directory as .git folder 
 +Start a new git project:  
 +<file bash>git init 
 +git config user.name "user_name" 
 +git config user.email "email"</file> 
 +Make a copy of a git (target can be specified either locally or remotely, via any number of protocols):  
 +<file bash>git clone target</file> 
 +Commit changes to a git:  
 +<file bash>git commit -m "message"</file> 
 +Get info on current repository:  
 +<file bash>git status</file> 
 +Show change log for current repository:  
 +<file bash>git log</file> 
 +Update git directory from another repository:  
 +<file bash>git pull [target]</file> 
 +Push branch to other repository:  
 +<file bash>git push [target]</file> 
 +Create a new branch:  
 +<file bash>git branch [branchname]</file> 
 +Switch to target branch:  
 +<file bash>git checkout [branchname]</file> 
 +Delete a branch:  
 +<file bash>git branch -d [branchname]</file> 
 +Merge two branches:  
 +<file bash>git merge [branchname] [branchname]</file> 
 +Show all branches of a project:  
 +<file bash>git branch 
 +=== MySQL === 
 +Get help:  
 +<file bash>help</file> 
 +Show databases:  
 +<file bash>show databases;</file> 
 +Choose a database to use:  
 +<file bash>use database/name/here;</file> 
 +Show database schema:  
 +<file bash>show tables;</file> 
 +Delete database:  
 +<file bash>DROP DATABASE databasename;</file> 
 +New database:  
 +<file bash>CREATE DATABASE databasename;</file> 
 +Create a new user:  
 +<file bash>CREATE USER username@localhost IDENTIFIED BY 'password';</file> 
 +Show users:  
 +<file bash>select * from mysql.user;</file> 
 +Delete a user:  
 +<file bash>delete from mysql.user WHERE User='user_name';</file> 
 +Give user access to all tables (make them root). the "%" means that they can sign in remotely, from any machine, not just localhost.:  
 +<file bash>grant all privileges on /./ to someusr@"%" identified by 'password';</file> 
 +give certain privileges to a user on a certain database:  
 +<file bash>grant select,insert,update,delete,create,drop on somedb.* to someusr@"%" identified by 'password';</file> 
 +Tell mysql to use new user priv policies:  
 +<file bash>flush privileges;</file> 
 +change user password:  
 +<file bash>use mysql; 
 +update user set password=password('newpassword') where User='user_name';</file> 
 +mysql command line args:  
 +export text file with commands to rebuild all mysql tables:  
 +<file bash>mysqldump databasename > dumpfilename.txt</file> 
 +restore from a dump:  
 +<file bash>mysql -u username -p < dumpfilename.txt</file> 
 +dump entire database 
 +<file bash>mysqldump -u username -p --opt databasename > dumpfile.sql</file> 
 +restore from entire database dump:  
 +<file bash>mysql -u username -p --database=databasename < dumpfile.sql</file> 
 +==== Virtualization ==== 
 +clone a virtual machine (this works, it's been tested):  
 +<file bash>vboxmanage clonehd virtual/machine/name.vdi --format VDI ~/target/virtual/machine_name.vdi</file> 
 +mount a shared virtual folder:  
 +you need to make sure you have the right kernel modules. You can do this with modprobe, but this package works instead in a ubuntu-specific way.  
 +<file bash>sudo apt-get install virtualbox-ose-guest-utils 
 +sudo mount -t vboxsf name/of/shared/folder/specified/in/Virtualbox path/of/mountpoint
 </file> </file>
-Now if you divide each file size by 1024 (1k) you will see that the first output is always lower than 4096 (4k) and upper 3072 (3k), 
-on the second output you have it between 4096 (4k) and 5120 (5k). 
-===== Find files with a give name and any extension =====  
-<file bash>find -name '[given_name].*'</file> 
-This will output the files of any given name but with any extension 
-===== Find files modified in the latest blocks of 24 hours ===== 
-<file bash>find -mtime n</file> 
-Where n is: 0 for the last 24 hours 1 for the last 48 hours 2 for the last 72 hours and so on. 
-===== Find files that was accessed in the latests blocks of 24 hours ===== 
-<file bash>find -atime n</file> 
-Where n is: 0 for the last 24 hours 1 for the last 48 hours 2 for the last 72 hours and so on. 
 linuxfind.txt · Last modified: 2021/05/12 22:14 by bpienig
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Recent changes RSS feed Driven by DokuWiki