Linux

Linux related stuff goes here :)

General Links

Debian

Slackware

Ubuntu

CygWin

Information about Cygwin goes here

Commands

All linux commands are discussed here

SED

SED Tutorial is given here -> http://www.grymoire.com/Unix/Sed.html

System Administration

Understanding the Path Variable

As in DOS, the shell uses the PATH variable to locate a command. PATH contains a list of directories separated by colons:

echo $PATH
/bin:/usr/bin:/usr/local/bin

When you enter a command, the shell looks in each of the directories specified in PATH to try to find it. If it can't find the command in any of those directories, you'll see a "Command not found" message.

If you decide to put your own programs in a bin directory under your home directory, you'll have to modify the path to include that directory, or the system will never find your programs (unless you happen to be in that directory when you enter the command). Here's how to change your PATH variable so it includes your personal bin directory:

PATH=$PATH:$HOME/bin

So if PATH was set to /bin:/usr/bin:/usr/local/bin beforehand, it would now have the value /bin:/usr/bin:/usr/local/bin:/home/hermie/bin.

Creating Your Own Shell Variables

If you are a programmer, you'll find it handy to create your own shell variables. First issue the command

code=$HOME/projects/src/spew

and then, regardless of what directory you are in, you can issue

cd $code

to pop over quickly to the directory containing the source code for that way-cool spew program you're developing. (The cd command means "change directory.")

A variable assignment like this will work just fine, but its scope (visibility) is limited to the current shell. If you launch a program or enter another shell, that child task will not know about your environment variables unless you export them first.

Unless you know for sure that an environment variable will have meaning only in the current shell, it's a good idea to always use export when creating variables to ensure they will be global in scope—for example,

How to find the DNS name in Linux

You can issue commands like domainname, dnsdomainname [it definitely works!]

Some good links are here:

* From About.com is here
* Further more, a google search on linux command to find the DNS name works :). It is here

How to find the gateway in Linux

Type "route -n" command in the shell.

How to get the environment, cpu details in Linux

Type "more /proc/cpuinfo" in the shell. 'cpuinfo' is the executable located in the /proc directory

The sample info i got it from LMRA Prod Was-P machine is here:

lmrwas-p:~ # more /proc/cpuinfo
vendor_id : IBM/S390
# processors : 1
bogomips per cpu: 1992.29
processor 0: version = FF, identification = 011EBC, machine = 2086

How to get the OS (specific flavour) and its version in Linux

Type "more /etc/issue" in the shell. It will give you the specific flavor of linux got installed and its version.

The sample info i got it from LMRA Prod Was-P machine is here:

lmrwas-p:~ # more /etc/issue

Welcome to SUSE LINUX Enterprise Server 9 (s390x) - Kernel \r (\l).

How to get the memory usage in Linux

Type "more /proc/meminfo" in the shell. It will give you the detailed list of memory like its total size available, memory used at the moment, free memory, etc .,

The sample info i got it from LMRA Prod Was-P machine is here:

lmrwas-p:/home/lmrschdlr/EMSAPPSchedular # more /proc/meminfo

MemTotal: 2050136 kB
MemFree: 947868 kB
Buffers: 13836 kB
Cached: 141712 kB
SwapCached: 4412 kB
Active: 887624 kB
Inactive: 79608 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 2050136 kB
LowFree: 947868 kB
SwapTotal: 2403256 kB
SwapFree: 2391668 kB
Dirty: 296 kB
Writeback: 0 kB
Mapped: 823904 kB
Slab: 112928 kB
Committed_AS: 1437476 kB
PageTables: 2692 kB
VmallocTotal: 4292861952 kB
VmallocUsed: 3152 kB
VmallocChunk: 4292858276 kB

One more thing, for a short output, you can use "free -m" command, it will give you the precise output of memory and swap spaces. Remember: The size given are in KBs. You got to convert them into MBs and GBs as required. :)

lmrwas-p:~ # free -m

total used free shared buffers cached
Mem: 2002 1087 914 0 14 138
-/+ buffers/cache: 934 1067
Swap: 2346 11 2335

How to sort the files based on the size?

* http://www.unixforum.co.uk/index.php?showtopic=7724
* http://www.yaroman.com/2007/08/14/how-to-sort-files-and-folders-by-size/
* http://www.ducea.com/2006/05/14/tip-how-to-sort-folders-by-size-with-one-command-line-in-linux/
* http://www.ubuntugeek.com/how-to-sort-files-and-folders-by-size.html
* http://puretechie.blogspot.com/2007/03/how-does-one-sort-files-based-on-size.html
* http://adminlinux.blogspot.com/2006/12/sort-folders-by-size-with-one-command.html

How to avoid the control characters when executing a shell script?

At times we get an error like /bin/bash^M: bad interpreter: no such file or directory when we run a shell script or any executable in Linux. The reason being the file would have got opened/edited in Windows OS there some control characters like Ctrl+M got added at the end. The interpreter (eg. bash) could not locate such characters in Linux OS. Hence the error. You can avoid this by running "dos2unix <filename>" which removes those characters.

Have a look at this url for more info: http://www.linuxquestions.org/questions/slackware-14/binbashm-bad-interpreter-no-such-file-or-directory-461400/

How to clean up the disk in Unix box when the root partition becomes full?

It is a tedious task of any Administrative activity. Have a look at the nice url -> http://www.linuxquestions.org/questions/slackware-14/root-partition-full-644838/

How to find the previous commands typed?

* You can can get to know the previous commands one by one by making use the arrow keys (up and down arrow keys).
* If you want to see the whole set of commands executed earlier, you can use history command.
* You can even set a limit on it by using "set history=<numberOfCommandsToStore>"

How to find files based on date?

* Good link with explanation -> http://www.cyberciti.biz/tips/shell-script-to-open-files-based-on-date.html

How to run a non-interactive command over web?

Try using 'wget' command. It is explained here -> The non-interactive network downloader http://linux.die.net/man/1/wget

How to check the file size omitting the sub-directories?

You can use the following command: "du -h | grep -v '/' | awk '{print $1}' "

  • You can also have a look at this URL -> Root folder maintenance at http://www.unix.com/sco/38257-root-folder-space-maintenance-sco.html
  • find command for the same -> "find / -type f -size +20000k > outputfiles.txt" —> to list out the files in outputfiles.txt having >20 MB Size
  • du command -> //"du -a |sort -r -n >/u/file.list" —> The output of this is a list of the files and directories on the entire system sorted in descending order by size. Using this you should be able to find files that you didn't know existed inadvertently saved in the wrong directory), and which files are growing (have a recent file date).

How to find the file type in linux?

  • see the file command

How do I list only directories?

How do I search for a specific pattern in a file?

You can use 'grep' command as follows:

"grep <patternToSearch> <fileName>"

It lists the matching lines having the <patternToSearch> in it. You can look for options about case sensitive.

How do I search for a specific pattern in all the files in a directory?

  "grep <patternToSearch> * "

—> '*' - for all files in the present directory

You can combine/redirect the output to "uniq" command or "sort -u" to get the unique entries if at all a single file having more than one entry matching with the same pattern. It would be as follows

"grep <patternToSearch> * | sort -u"
"grep <patternToSearch> * | uniq"

How do I get only time from the 'date' command?

You can use the 'cut' command to extract a specific field/column/character from the set of text. The command would be

date | cut -d' ' -f4

The output displayed would be

11:34:49

If you don't want the semicolon (:) [typical usage would be when you would like to get the timestamp and append it to the filename as <fileName_HHMMSS.txt>], you can use 'tr' command (translate) with -d option (to delete) for ":" as follows:

date | cut -d'  ' -f4 | tr -d ':'

The output would be

113449

How to extract a specific field in ls -ltr command — say only the user or group?

You can use the combination of 'tr' and 'cut' commands as follows: looks like a crude/indirect way (got to know from Supreeth of THBS)

ls -ltr | tr -s '  ' ? | cut -d? -f3

The above command translates the whitespaces with a question mark (?) using 'tr' command with '-s' option and then uses 'cut' command for extracting the 3rd field (-f3) in the output using the delimiter (-d?) as the question mark.

You can use -f4 for getting the group name.

How to set the system date/time?

http://www.linuxsa.org.au/tips/time.html

How to extract all public methods in set of .java files?

This was basically asked in BOJUG google groups and the below ones are the suggestions given by the user.

1.

% find . -name \*.java -print | xargs grep "^public "

2. You could get a list of all the files in the source folders and use reflection to figure out public methods.

Class c = Class.forName(filePathCorrectlyConvertedToFQName);
Method m[] = c.getMethods();

Also have a look at http://java.sun.com/developer/technicalArticles/ALT/Reflection/

If I were in a hurry and if all I wanted was to estimate, I would use Simran's method. (the above one of using 'find' command)

How to Copy Just the Last Two Days of Files to Another Directory

http://www.howtogeek.com/wiki/Copy_Just_the_Last_Two_Days_of_Files_to_Another_Directory

How to set/change the Default Gateway in a Linux machine

  • Alternate: Use the route command as follows

[[code type="java"]]
route add default gw 192.168.0.1
[[/code]

How do run a command as a specific user in Unix via my Java Program?

Shell Scripting

Some good links:

https://fedorahosted.org/liveusb-creator

Shell Scripting - HowTos

  • How to assign a output of a command to a variable?
  • You have to embed the command within a backtick (with tilde key).

Example :

myOutput = `basename $filename .bat`
cat myOutput

The above code extracts the basename of a file (without any extension) and assigns the result of the command to the variable 'myOutput'.

  • How to get the filename without extension?
  • How to get the extension of a filename?
  • How to get the list of files in a specific directory which you pass an argument?

Cron Jobs

Tips and Tricks

IP tables

VI editor

Misc

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.