Linux Index
What is a heredoc and what are the benefits of using them?
On this page... (hide)
- 1. Add a user to a group
- 2. Display contents of file with each line numbered
- 3. My Linux Cheat Sheet
- 4. Find directories that have files that match a pattern
- 5. My Favorite Aliases (BASH syntax)
- 5.1
sudoversions
- 5.1
- 6. Some BASH notes about shell scripting
- 7.
g++script - 8. Using
curlorwgetto resume downloads - 9.
datecommand - 10. Finding big files
- 11. Testing the load time of a URL
- 12. Change your login shell
- 13. Determining which shell you are using.
- 14.
lsusb - 15.
niceandrenice - 16. Brute for rounding after using
bc - 17. Some listing (
ls) options (when you can't list the contents of a symbolic link) - 18. How to lock a users account.
- 19. Cool
sedcommand for Firefox - 20.
scp - 21.
mcedit - 22.
tee - 23. Error while loading shared libraries:
libiconv.so.2: cannot open shared object file: No such file or directory - 24. SSH without a password (public key authentication)
- 25. ssh-keygen at aplus.net
- 26. PHP in a shell script
- 27. Doing multiple things at the command line at one time
- 28.
bashshell - 29. If statment
- 30. SSH using public key
- 31. Page Comments (Click to edit)
1. Add a user to a group
usermod -a -G group_name user_name
2. Display contents of file with each line numbered
$ cat -n myfile.txt
3. My Linux Cheat Sheet
4. Find directories that have files that match a pattern
Replace *.mp3 with a string that will be used to match file below where you're at.
$ find . -name '*.mp3' | sed 's/^\.\/\([^/]*\)\/.*$/\1/g' | uniq
5. My Favorite Aliases (BASH syntax)
alias rm="rm -i" alias mv="mv -i" alias cp="cp -i" alias ls="ls -F" alias ll="ls -l" alias ltr="ls -ltr" alias pico="pico -r3000"
5.1 sudo versions
alias rm="sudo rm -i" alias mv="sudo mv -i" alias cp="sudo cp -i" alias ls="sudo ls -F" alias ll="sudo ls -l" alias ltr="sudo ls -ltr" alias pico="sudo pico -r3000" alias vi="sudo vi"
6. Some BASH notes about shell scripting
7. g++ script
This script takes a filename without an extension, compiles it with g++ and returns an executable with the same filename but with the .exe extension for reference only -- has nothing to do with Windows.
There really isn't anything fancy other than shortening something I do very often.
#!/bin/bash
if [[ "${1}" == "" || "${1}" == "-h" || "${1}" == "--help" ]]; then
echo "
Usage: mygcc <name of C++ source file without extension>
Source file should have the extension of .C only.
This will output a file called: <C++ source file name>.exe
I've used .exe just so that you know it's an executable file.
It has nothing to do with Windows. ;)
"
exit 1;
fi
g++ ${1}.C -o ${1}.exe
8. Using curl or wget to resume downloads
8.1 wget
$ wget -c <url of file to download>
8.2 curl
$ curl -C - -L -O <url of file to download>
9. date command
To set the system clock back 1 hour, issue the following command.
# date --set='-1 hour'
10. Finding big files
The following command will display all files that are in the megabyte range.
$ du -sh * | grep "^[0-9]*M"
The next command will display all files that are in the gigabyte range.
$ du -sh * | grep "^[0-9]*G"
If you're looking for something like kilobytes or petabytes ... etc, just substitute the M or G with the corresponding character. K is for kilobytes. I'm not sure of the others. Probably P for peta, and T for terabytes.
11. Testing the load time of a URL
The quickest way to do this is with the time and curl commands. Here is the basic syntax I use.
$ time curl "http://www.domain.org" > /dev/null
You don't have to use the quotations, but if you have a query string it may complain without the quotes. Also, you can omit the > /dev/null. That only suppresses the page output since we're only interested in the load time.
12. Change your login shell
Some distributions may have the command chsh. Issue the following command to change your shell to BASH.
$ chsh -s /bin/bash username
13. Determining which shell you are using.
If you'd like to determine the shell you are using, use the following command.
finger -m username
14. lsusb
This command displays devices connected to your computer. Below is some sample output. All of these happen to be usb devices.
Bus 003 Device 003: ID 046d:d001 Logitech, Inc. QuickCam Pro Bus 003 Device 002: ID 058f:9360 Alcor Micro Corp. 8-in-1 Media Card Reader Bus 003 Device 001: ID 0000:0000 Bus 002 Device 002: ID 0461:4d03 Primax Electronics, Ltd Kensington Mouse-in-a-box Bus 002 Device 001: ID 0000:0000 Bus 001 Device 001: ID 0000:0000 Bus 001 Device 003: ID 1058:0901 Western Digital Technologies, Inc. Bus 001 Device 005: ID 152e:e003 LG (HLDS)
15. nice and renice
These two commands are very handy for setting program priorities. -20 is the highest or nicest and 19 is the lowest. All you have to do is run your program with nice in front of it.
# nice -15 your_program
The example above uses 15 which is pretty high priority. If I had used,
# nice -+15 your_program
this would be a very low priority. This is like golf, low scores are high.
To change a priority, use renice. Just find the pid of your program. I do this with
# ps -edalf | grep "my_program"
Your pid will be the 4th column, or the number after your username which should be the 3rd column. -edalf doesn't work on all distributions. It definitely doesn't work on the Mac, in which case I use -aux. Just read your man pages and find the pid.
Once you have your pid, you can use the command
renice -20 pid_of_your_program
16. Brute for rounding after using bc
This one rounds to 3 decimal places. Three "dots" in the regex ---> sed 's/^\(.*\.{span:style:background-color:black; color:white;/}...{/span}\).*$/\1/g'. ADd or remove dots to change the rounding.
me@host:~$ echo "5 / 27" | bc -l | sed 's/^\(.*\....\).*$/\1/g' .185
Note: This really isn't rounding, it just looks like it.
17. Some listing (ls) options (when you can't list the contents of a symbolic link)
If you can't list the contents of a directory that is a symbolic link, this can be remedied by adding the -H option, or removing a conflicting option -- such as -d.
18. How to lock a users account.
A normal user joe would have an entry in /etc/passwd such as,
joe:x:1000:100:Joe Smith,,,:/home/joe:/bin/bash
To lock this account, all you have to do is replace the :x: with :*: as in
joe:*:1000:100:Joe Smith,,,:/home/joe:/bin/bash
19. Cool sed command for Firefox
This command converts all your bookmarks into a nice list.
$ sed 's/^.*<DT><A HREF="\(.*\)" ADD.*>\(.*\)<\/A>.*$/<a href="\1">\2<\/a>/g' bookmarks.html
$ sed '/DOCTYPE/d' bookmarks2.html > bookmarks3.html; mv -f bookmarks3.html bookmarks2.html $ sed '/automatically generated file/d' bookmarks2.html > bookmarks3.html; mv -f bookmarks3.html bookmarks2.html $ sed '/will be read and overwritten/d' bookmarks2.html > bookmarks3.html; mv -f bookmarks3.html bookmarks2.html $ sed '/DO NOT EDIT/d' bookmarks2.html > bookmarks3.html; mv -f bookmarks3.html bookmarks2.html $ sed '/META HTTP/d' bookmarks2.html > bookmarks3.html; mv -f bookmarks3.html bookmarks2.html $ sed '/TITLE>Bookmarks/d' bookmarks2.html > bookmarks3.html; mv -f bookmarks3.html bookmarks2.html $ sed '/LAST_MODIFIED/d' bookmarks2.html > bookmarks3.html; mv -f bookmarks3.html bookmarks2.html $ sed '/<DL><p>/d' bookmarks2.html > bookmarks3.html; mv -f bookmarks3.html bookmarks2.html $ sed '/<DT><H3/d' bookmarks2.html > bookmarks3.html; mv -f bookmarks3.html bookmarks2.html $ sed '/<DD>/d' bookmarks2.html > bookmarks3.html; mv -f bookmarks3.html bookmarks2.html
The first line is fine, but the rest isn't working -- I need to re work it.
20. scp
20.1 Copying a directory to another
The directory or file can be copied to another location or will overwrite another file.
$ scp -r a_directory a_destination
20.2 Copy multiple files in different locations to a destination
$ scp file.txt joe@ahost.org:/path/to/me.txt another.tex a_directory/ \ joe@ahost.com:~/my_best_file.txt a_destination
21. mcedit
This is a nice text editor to try out. It's pretty neat because it responds to the mouse. You can even scroll the screen with the mouse wheel.
22. tee
You can use tee to copy text from a piped command into a file and to the stdout at the same time instead of echo'ing to a file and then echo'ing to the stdout.
To use tee, just type the command below, and anything you type and hit enter will be added to myfile.txt. If you add the option -a you will just append text to the file.
$ tee myfile.txt
23. Error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory
Okay, while you may be running into a slightly different error, if you can't load a shared library while running commands, such as compiling packages, starting services or running software ... etc, you may be able to remedy this by doing the following.
First, make sure the shared library exists - in this case it is libiconv.so.2.
# locate libiconv.so.2
or
# find / -name 'libiconv.so.2'
The later is much slower if you don't have slocate installed or an equivalent.
Once you know the library exists, you can load it if you have permissions via,
# ldconfig
24. SSH without a password (public key authentication)
SSH without passwords - generating keys
Entering passwords upon every SSH connection can be annoying. On the other hand, unprotected remote connection is a huge security risk. The solution to this problem is authorization using the private-public key-pair.
The pair of keys is usually generated using the ssh-keygen command. Below, there is a sample effect of such key generation. RSA or DSA keys can be used.
$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/user1/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user1/.ssh/id_rsa. Your public key has been saved in /home/user1/.ssh/id_rsa.pub. The key fingerprint is: xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
When the program asks for the key password, we should just press ENTER - this way, a passwordless key will be created. Remember that this is always a security hole to have a passwordless key (in simple words, doing that downgrades your remote system security to the security of your local system) so do it on your own risk. As the ssh-keygen finishes its work, you can see that two keys have been generated. The private key landed in /home/user1/.ssh/id_rsa and we should never make this public. The second public key appeared in /home/user1/.ssh/id_rsa.pub and this is the one we can show the entire world.
Now, if we want to access a remote system from our local computer without passwords (only using the keys), we have to add the information about our public key to the authorized_keys file located in ~/.ssh folder on the remote system. This can be done using the following commands:
$ scp /home/user1/.ssh/id_rsa.pub \ user1@remote_server:~/ $ ssh user1@remote_server $ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
The third command will be obviously executed on a remote server. After this operation, all actions performed on the remote server with SSH will not need any password whatsoever. This will certainly make our work easier.
http://en.jakilinux.org/apps/ssh-tricks/ »
25. ssh-keygen at aplus.net
For aplus.net, I have to issue the command ssh-keygen -t dsa -b 1024.
Then I take the public key from inside ~/.ssh/id_dsa.pub and I have to put it in my authorized_keys2 file on the aplus.net server.
You can only put the key on the server via the aplus web console.
26. PHP in a shell script
Just do a simple echo of the script, and pipe to php or php -q
echo '<?php print("hello world\n"); ?>' | php
A more useful example is using this in a bash script with here documents, that way you don't have to escape characters.
#!/bin/bash
( cat <<eof
<?php
print("hello\n");
?>
eof
) | php
If you want to add variables such as $str = "hello world"; you have to escape the dollar sign. Here is the script.
#!/bin/bash ( cat <<eof <?php $str = "hello world\n"; print($str); ?> eof ) | php
27. Doing multiple things at the command line at one time
Here's how to make the directories dir1, dir2, dir3, dir4 really quick.
# mkdir dir{1,2,3,4}
28. bash shell
A nice PS1
PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '
29. If statment
#!/bin/bash #> error and success are two files # error="No pages of output" # success="Output written on" if grep "No pages of output" error > /dev/null; then echo "We found 'No pages of output' in error" fi #> out puts the echo above if grep "written on" success > /dev/null; then echo "We found 'Output written on' in success" fi
30. SSH using public key
Normally when I ssh to a host, I use a command like ssh name@www.host.org and it will prompt for a password and then I'm in. If you use a public key, you can generate it with
$ ssh-keygen -b 512 -t dsa
where -b 512 could be 512 or 1024 bits in length and -t dsa could be dsa or rsa. So, after doing this, you can now ssh to a host with just
ssh www.host.org
and it won't even prompt for a password (security risk) as long as you don't give it a passphrase when it prompts with ssh-keygen. You may want to read up on this or man ssh-keygen for more help.
