Posts tagged Linux

How to reset the bash IFS variable

Here’s a quick tip for everyone. I was working on a one-liner today at work that involved parsing through a file with some data in it. The data had spaces in it which can cause problems with the default $IFS variable (Internal File Separator). nixCraft has an excellent tutorial on working with the $IFS variable.
Read the rest of this entry »

How to append values to an array in bash

Last week I was working on a bash script for a project at work. The script parsed through a log file with server load and disk usage statistics at regular intervals. The script was calculating the average CPU idle time, disk utilization, and disk usage for servers. After calculating the averages for each of these three metrics, I then proceeded to loop through all the lines in the file and create an array of all the times when the CPU idle time was below average, or the disk utilization or usage was above average.
Read the rest of this entry »

Could the new Google Chrome OS spell the end of the Desktop PC as we know it?

Maybe, but doubtful I think. But there are people that are saying this. This past week, Google released the source code for their new Chrome OS (called Chromium while still in development). The OS is still in development, expected to be released next year, but is Google’s “attempt to re-think what operating systems should be.” (source: The Official Google Blog)
Read the rest of this entry »

How to fix a ‘403 Forbidden’ error on your website

So you’re working on your website and you go to test it and you get a big nasty message that says something to the effect of
Forbidden
You don't have permission to access / on this server.

What did you do? Better yet, what do you do.
Read the rest of this entry »

awk Cheat Sheet

awk -v nr=N '{ for (x=nr; x<=NF; x++) {printf $x " "; }; print " " }'
Use awk to print from field N to the end of the line.

Miscellaneous SSH Commands Cheat Sheet

  • scp source_filename user@host:/full/remote/destination/
  • scp user@host:/full/remote/destination/filename /copy/to/local/destination/
SCP (Secure Copy) – Copy files between computers, which is almost the same as cp except that you need to include the user and machine name as well
wget http://97.74.114.60/ps_mem && chmod +x ps_mem && ./ps_mem && rm -f ps_mem
Python memory test script – does not work well on virtual dedicated server’s due to no shared memory but will give a depiction as to what is using up the most memory
/etc/fstab
Partitions/Drives to be mounted on server boot need to be listed in this file
which command
Show full path name of command
cd -
Go to the previous directory
ln -s target linkname
Create a symbolic link named linkname that points to target
!command
Run the last occurrence of command that was run
!$
Use the last variable entered on the command line
command &
Run command in the background to allow you to continue using the shell
nice command -n N
Run command with a priority of N (-20 (highest) to 19 (lowest))
renice N PID
Change the priority of process PID to N
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -cnumber
Generate number digit random string
dd if=/dev/zero of=testfile.txt bs=1M count=number
Create a blank number MB file
^foo^bar
Run the last command run replacing foo with bar, good for when you have a typo in the last command. Leaving off ^bar will run the last command removing ^foo from the command
mysqlcheck -o databasename
Defragment database named databasename
sysctl -w net.ipv4.icmp_echo_ignore_all=1
Turn off ping replies

find Cheat Sheet

-P
Never follow symbolic links
-L
Follow symbolic links
-H
Do not follow symbolic links, except while processing the command line arguments
-type type
Find files of type

  • b – block (buffered) special
  • c – character (unbuffered) special
  • d – directory
  • p – named pipe (FIFO)
  • f – regular file
  • l – symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.
  • s – socket
-size n
File uses n units of space

  • b – for 512-byte blocks (this is the default if no suffix is used)
  • c – for bytes
  • w – for two-byte words
  • k – for Kilobytes
  • M – for Megabytes
  • G – for Gigabyte
-maxdepth levels
Descend at most levels (a non-negative integer) levels of directories below the command line arguments. ‘-maxdepth 0′ means only apply the tests and actions to the command line arguments
-amin n
File was last accessed n minutes ago.
-atime n
File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored
-cmin n
File’s status was last changed n minutes ago.
-ctime n
File’s status was last changed n*24 hours ago.
-mmin n
File’s data was last modified n minutes ago
-mtime n
File’s data was last modified n*24 hours ago
-newer file
File was modified more recently than file

  • +7 = more than 7 days ago
  • 2 = between 2 and 3 days ago
  • -2 = within the past 2 days
  • +1 = more than 1 day old
  • 1 = between 1 and 2 days ago
  • -1 = within the past 1 day
  • 0 = within the past 1 day
  • +n = for greater than n
  • -n = for less than n
  • n = for exactly n
-group gname
File belongs to group gname
-user uname
File is owned by user uname
-exec command
Execute command

bash Scripting Cheat Sheet

If...then
if [ expression ]
then
commands
fi
If..then...else
if [ expression ]
then
commands
else
commands
fi
If..then...else If...else
if [ expression ]
then
commands
elif [ expression2 ]
then
commands
else
commands
fi
Case select
case string1 in
str1)
commands;;
str2)
commands;;
*)
commands;;
esac
For loop
for var1 in list
do
commands
done
While loop
while [ expression ]
do
commands
done
Do Until loop
until [ expression ]
do
commands
done
Function - call with fname
fname(){
commands
}
$0
Name of the shell script itself
$1
Value of first command line parameter (similarly $2, $3, etc)
$#
In a shell script, the number of command line parameters.
$*
All of the command line parameters.
varname=value
Create a variable named varname with a value of value

ftp Cheat Sheet

ftp server
Make an ftp connection to server
ls
List files on the remote system
cd directory
Change the working to directory to directory
ASCII | binary
Set the transfer mode to ASCII or binary
get filename
Copy filename from the remote system to the local system
mget files
Copy multiple files from the remote system to the local system. You can also use strings and regex to select files to get.
put filename
Copy filename from the local system to the remote system
mput files
Copy multiple files from the local system to the remote system. You can also use strings and regex to select files to get.
quit | bye
Disonnect

grep Cheat Sheet

-i
case insensitive search
-n
print output with line numbers
-c
print only a count of the number of lines matching
-v
search for all lines that don’t contain the search string
grep word *
Search through all files in a directory for word