Skip to content
E. Lynette Rayle edited this page May 25, 2024 · 5 revisions

External References


Writing scripts for cd

NOTE: cd scripts won't work correctly from bin. Need to add them as an alias in .bashrc.

alias cdr="cd ~username/htdocs/rails/;pwd"

.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:~username/bin"

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
sudo sudo mkdir test run command as root
sudo -s open a root shell
sudo -s -u user sudo -s -u net33 open a shell as user
type cmdname shows you the type of a command which is the executable location for programs
whereis filename whereis mysql shows location of all files with that name
whoami whoami shows the currently logged in user
scp -r localfilename remoteuser:remotedir scp -r ~/uploads/.env username@my.server.com://var/app/current secure copy from local to remote machine NOTE: -r is only needed for recursive copy of an entire directory
scp -r username@remote-host:/path/to/directory /local/path/to/where/you/want/to/put/it scp -r username@my.server.com://var/app/current/.env ~/downloads secure copy from remote to local machine
echo $SHELL echo $SHELL show which shell is running
grep -r pattern path 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 pattern path | wc -l grep "" bib.xml | wc -1 Count the number of matches for grep. In the example, it will count the number of times that appears in file bib.xml.

Working with Services

Action Command Comments
start /etc/inid.d/service_name start ex. sudo /etc/init.d/app-018.serverfarm.mine.com start
stop /etc/inid.d/service_name stop
restart /etc/inid.d/service_name restart
status /etc/inid.d/service_name 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

Reference: findcmd.htm

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: show-all-running-processes-in-linux

lists PID of all running processes that have the pattern (mysqld in the example) in the COMMAND; matches mysqld and mysqld_safe

pgrep mysqld

lists PID of the specific process (mysqld in the example); matches mysqld only; will not match mysqld_safe

pidof mysqld

shows top running processes (according to CPU usage); show enough to fill the size of the window; continues to update

top

_NOTE: q OR to 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 username

Reference: 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

NOTE: You will have to use sudo if you didn't start the process.


Resuming a paused job after

Reference: how-can-i-resume-a-stopped-job-in-linux

$ 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.


Symbolic Links

Add

ln -s {real-filename} {symbolic-filename}

Remove

rm {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 -br folder1/ folder2/

where,

  • -r = recursive
  • -b = ignore space changes

ignoring a directory

diff --exclude=.git -r 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

head

# show first 10 lines
head -n 10 filename

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: 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
Clone this wiki locally