External References
- Linux/UNIX Command Ref
- Advanced commands (mostly ubuntu oriented)
- .bash_profile vs .bashrc by Josh Staiger
- A Unix/Linux “find” Command Tutorial by Wayne Pollock
- Show all running processes in linux by nixCraft
- Kill Process in Linux or Terminate a Process in UNIX / Linux Systems by nixCraft
Writing scripts for cd
alias cdr="cd /cul/web/elr37-dev.library.cornell.edu/htdocs/rails/;pwd"
Reference: .bash_profile vs .bashrc by Josh Staiger
NOTE: Requires .bash_profile to have the following code.
if [ -f ~/.bashrc ]; then source ~/.bashrc fi
Updating $PATH
# view PATH echo $PATH # update PATH PATH=$PATH\:/dir/path ; export PATH
To make this change persistent, add the following to .bash_profile
export PATH="$PATH:~elr37/bin"
NOTE: cd scripts won't work correctly from bin. Need to add them as an alias in .bashrc. See notes under Writing scripts for cd.
Append a line to a text file
echo "line to append" >> path/to/file echo "gem: --no-document" >> ~/.gemrc
Check file's md5
CYGWIN
- md5sum FILE_NAME
Mac terminal
- openssl md5 FILE_NAME
sudo, type, whereis, whoami, scp, echo, grep
Command | Example | Action | Comments |
---|---|---|---|
|
| run command as root | |
| open a root shell | ||
|
| open a shell as user | |
| shows you the type of a command which is the executable location for programs | ||
|
| shows location of all files with that name | |
|
| shows the currently logged in user | |
scp -r elr37@remote-host:/path/to/directory /local/path/to/where/you/want/to/put/it |
| secure copy from local to remote machine | NOTE: -r is only needed for recursive copy of an entire directory |
|
| show which shell is running | |
| grep -r foo * | Starting at current directory, look for 'foo' in all files including subdirs. | NOTE: -r is only needed for recursive search through subdirs |
| grep "<record>" bib.xml | wc -1 | Count the number of matches for grep. | In the example, it will count the number of times that <record> appears in file bib.xml. |
Working with Services
Action | Command | Comments |
---|---|---|
start |
| ex. sudo /etc/init.d/tomcat-sf-lib-app-018.serverfarm.cornell.edu start |
stop |
| |
restart |
| |
status |
|
OR
Action | Command | Comments |
---|---|---|
start | sudo service service_name start | ex. sudo service mysqld start |
stop | sudo service service_name stop | |
restart | sudo service service_name restart | |
status | sudo service service_name status |
Find Command
find where-to-look criteria what-to-do # prints names of files that directly match foo (e.g. /usr/foo, /usr/bin/foo) find / -name foo # prints names of files that contain foo (e.g. /usr/myfoo, /usr/bin/foobar) find / -name foo # suppress permission denied statements find /. -name *foo* 2>/dev/null
Tree command
$ tree . ├── contrib │ ├── analysis-extras │ │ ├── README.txt │ │ ├── lib │ │ │ ├── icu4j-49.1.jar │ │ │ ├── morfologik-fsa-1.5.5.jar │ │ │ ├── morfologik-polish-1.5.5.jar │ │ │ └── morfologik-stemming-1.5.5.jar │ │ └── lucene-libs │ │ ├── lucene-analyzers-icu-4.3.0.jar │ │ ├── lucene-analyzers-morfologik-4.3.0.jar │ │ ├── lucene-analyzers-smartcn-4.3.0.jar │ │ └── lucene-analyzers-stempel-4.3.0.jar │ └── extraction │ └── lib └── solr-analysis-extras-4.3.0.jar
NOTE: If you are on the mac and it says tree is not a command, run the following to install.
brew install tree
Create all directories on a path
# creates directories /my /my/path /my/path/here if they don't already exist mkdir -p /my/path/here/
pgrep, pidof, top, ps, kill
Reference: http://www.cyberciti.biz/faq/show-all-running-processes-in-linux/
pgrep mysqld lists PID of all running processes that have the pattern (mysqld in the example) in the COMMAND; matches mysqld and mysqld_safe pidof mysqld lists PID of the specific process (mysqld in the example); matches mysqld only; will not match mysqld_safe
top shows top running processes (according to CPU usage); show enough to fill the size of the window; continues to update q quit viewing top
ps show my processes ps u show userid that started each process ps a show processes for all users ps au show processes for all users and userid that started each process ps aux show processes for all users and userid that started each process and x adds processes started on different machines (tty) including services ps -U root -u root show all processes started under root user ps -U root -u root -N show all processes started by users other than root ps u -U root -u root -N show all processes started by users other than root and userid that started each process # see all the processes I started ps -U elr37
Reference: http://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/
Be very very careful what you kill.
$ pidof mysqld 4135 $ kill 4135
You will have to use sudo if you didn't start the process.
Resuming a paused job after <ctrl><z>
$ jobs [1]+ Stopped vi test $ fg 1
The #1 is identifying the job to resume in the case where there is more than one job paused. If there is only one paused job, then the number is optional.
Ref: how-can-i-resume-a-stopped-job-in-linux
Symbolic Links
ln -s {real-filename} {symbolic-filename}
Compressing and Extracting tar.gz and zip files
# extract to current directory tar -zxvf yourfile.tar.gz # extract to specified directory tar -C /myfolder -zxvf yourfile.tar.gz # zip file zip filename.zip file1 file2 file3 # zip directory zip -r filename.zip /myfolder # unzip unzip filename.zip
recursive diff
diff -bur folder1/ folder2/
tail
# show last few lines and exit tail /path/to/file # show last few lines as they update tail -f /path/to/file # show last n lines as they update tail -n 60 -f /path/to/file
du
# how much space is used in the current directory recursively du -h . 2>/dev/null
This will list all directories and subdirectories and show how many kB, MB, GB are used by each dir and it's subdirs.
# how much space is used in the current directory summed du --max-depth=1 -c -h 2>/dev/null
This will list all directories, but not subdirs. Totals will include space used by each directory + their subdirs.
Debugging full memory (RAM)
List processes that are using the most memory...
$ top <shift><m>
NOTE: <shift><m> sorts the top results by memory usage (i.e., %MEM)
$ free -m total used free shared buffers cached Mem: 16041 1298 14742 0 36 131 -/+ buffers/cache: 1130 14911 Swap: 0 0 0
Debugging full memory (disk space)
This will list all directories, but not subdirs. Totals will include space used by each directory + their subdirs.
cd / du --max-depth=1 -c -h 2>/dev/null
For largest directory, navigate to it and run the command again. Keep drilling down into directories until you find the one(s) that are eating up disk space.
On AWS, common problem locations:
- /var/app/current/tmp/network_cache
- /var/app/current/log
- /var/log