Daily Linux Commands

Familiarize yourself with Linux Commands

Monday, November 3, 2008

Commands to Retrieve Load, statistics and messages

# top                                # display and update the top cpu processes
# mpstat 1                           # display processors related statistics
# vmstat 2                           # display virtual memory statistics
# iostat 2                           # display I/O statistics (2 s intervals)
# systat -vmstat 1                   # BSD summary of system statistics (1 s intervals)
# systat -tcp 1                      # BSD tcp connections (try also -ip)
# systat -netstat 1                  # BSD active network connections
# systat -ifstat 1                   # BSD network traffic through active interfaces
# systat -iostat 1                   # BSD CPU and and disk throughput
# tail -n 500 /var/log/messages      # Last 500 kernel/syslog messages
# tail /var/log/warn                 # System warnings messages see syslog.conf
Read the rest of this entry >>

Hardware Information Accessing Commands

# cat /proc/cpuinfo                              # CPU model
# cat /proc/meminfo                            # Hardware memory
# grep MemTotal /proc/meminfo            # Display the physical memory
# watch -n1 'cat /proc/interrupts'           # Watch changeable interrupts continuously
# free -m                                                     # Used and free memory (-m for MB)
# cat /proc/devices                                     # Configured devices
# lspci -tv                                                 # Show PCI devices
# lsusb -tv                                              # Show USB devices
# lshal                                                  # Show a list of all devices with their properties
# dmidecode                                          # Show DMI/SMBIOS: hw info from the BIOS
Read the rest of this entry >>

Saturday, November 1, 2008

GCC Tips

View Compilation Plan

gcc -### <the rest of your command line goes here>

The GCC you run is a driver program for a bunch of other programs. With this parameter, gcc will produce (but not actually execute) the commands it would have used to accomplish the task you asked it to do. This way, you can see the gory details of what's going on behind the scenes. What library is being used? What is -mcpu set to? It's all there.

You can pipe this output to a file and execute that to compile a program, making it easy to experiment with tweaks to the linker or assembler.

Reading specs from /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/specs
Configured with: ../configure --prefix=/opt/timesys/toolchains/ppc7xx-linux --mandir=/opt/timesys/toolchains/ppc7xx-linux/share/man --infodir=/opt/timesys/toolchains/ppc7xx-linux/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++ --with-sysroot=/here/workdir/i386-x-ppc7xx/deleteme --disable-libgcj --build=i686-timesys-linux --host=i686-timesys-linux --target=powerpc-linux --program-prefix=ppc7xx-linux-
Thread model: posix
gcc version 3.4.1 20040714 (TimeSys 3.4.1-7)
/opt/timesys/toolchains/ppc7xx-linux/libexec/gcc/powerpc-linux/3.4.1/cc1 -quiet -v -D__unix__ -D__gnu_linux__ -D__linux__ -Dunix -D__unix -Dlinux -D__linux -Asystem=linux -Asystem=unix -Asystem=posix -I/opt/timesys/toolchains/ppc7xx-linux/powerpc-linux/include/nptl file.c -quiet -dumpbase file.c -auxbase file -version -o /tmp/ccShiHn4.s
ignoring nonexistent directory "/here/workdir/i386-x-ppc7xx/deleteme/usr/local/include"
ignoring nonexistent directory "/here/workdir/i386-x-ppc7xx/deleteme/usr/include"
#include "..." search starts here:
#include <...> search starts here:
/opt/timesys/toolchains/ppc7xx-linux/powerpc-linux/include/nptl
/opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/include
/opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/include
End of search list.
GNU C version 3.4.1 20040714 (TimeSys 3.4.1-7) (powerpc-linux)
compiled by GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5).
GGC heuristics: --param ggc-min-expand=47 --param ggc-min-heapsize=32138
/opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/bin/as -mppc -many -V -Qy -o /tmp/ccWeV3a3.o /tmp/ccShiHn4.s
GNU assembler version 2.15.90.0.3 (powerpc-linux) using BFD version 2.15.90.0.3 20040415
/opt/timesys/toolchains/ppc7xx-linux/libexec/gcc/powerpc-linux/3.4.1/collect2 --eh-frame-hdr -V -Qy -L/opt/timesys/toolchains/ppc7xx-linux/powerpc-linux/lib/nptl --rpath-link /opt/timesys/toolchains/ppc7xx-linux/powerpc-linux/lib/tls -m elf32ppclinux -dynamic-linker /lib/ld.so.1 -o file /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/lib/crt1.o /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/lib/crti.o /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/crtbegin.o -L/opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1 -L/opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/lib /tmp/ccWeV3a3.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/crtsavres.o /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/crtend.o /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/lib/crtn.o
GNU ld version 2.15.90.0.3 20040415
Supported emulations:
elf32ppclinux
elf32ppc
elf32ppcsim

Pre-Process, Retain Comments

gcc -C -E <file-name.c> -o file

Some engineers love to do coding in macros. The rest of us would like to break their fingers. This command will run the file through the pre-processor, expanding all macros, but retaining all comments. Stick a comment like "LOOK HERE" and search for that so you reduce the amount of time you spend looking for the offending code.

See what Files the Linker is Using

gcc -Wl,-t <parameters>

Displays what files the linker opens in what order. When looking in archive files, the archive file is displayed in para theses, followed by the file in the archive. Very handy when working through a legacy project that depends on files linking in a certain order that suddenly breaks because of a small (probably viewed as not noteworthy) change in a makefile somewhere.

/usr/bin/ld: mode elf_i386
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/../../../crt1.o
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/../../../crti.o
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/crtbegin.o
/tmp/cc37FxnS.o
-lgcc_s (/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/libgcc_s.so)
/lib/libc.so.6
(/usr/lib/libc_nonshared.a)elf-init.oS
-lgcc_s (/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/libgcc_s.so)
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/crtend.o
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/../../../crtn.o

Print Pre-defined Macros

gcc -E -dM - < /dev/null | cut -c 9- | sort

Very handy when porting code. Lets you know if your target processor has some missing defines or if something is different (line __INT_MAX__) that can have interesting effects on your project. Diff the output from the old to the new compiler so you can easily see the differences, makes it easy to spot problems before getting started.

Sample output, from a compiler targeting an ARM processor.

__APCS_32__ 1
__arm__ 1
__ARM_ARCH_4T__ 1
__ARMEL__ 1
__CHAR_BIT__ 8
__CHAR_UNSIGNED__ 1
__DBL_DENORM_MIN__ 4.9406564584124654e-324
__DBL_DIG__ 15
__DBL_EPSILON__ 2.2204460492503131e-16
__DBL_HAS_DENORM__ 1
__DBL_HAS_INFINITY__ 1
__DBL_HAS_QUIET_NAN__ 1
__DBL_MANT_DIG__ 53
__DBL_MAX_10_EXP__ 308
__DBL_MAX__ 1.7976931348623157e+308
__DBL_MAX_EXP__ 1024

Mixed Assembler and Source Output

gcc -g somefile.c -o somefile
objdump -S somefile 

Prints out each line in the program and the corresponding assembly code. Very handy when you're trying to see that the processor is generating the correct code, with the instructions you're expecting. You can also see the effects of optimization, but would recommend doing this for a small amount of code because when the optimization level is high, there's a much lower relationship between line of code and generated assembler.

Here's an example of what objdump produces for a few lines of code:

gpvSharedMemory = shmat(hSharedMemory, NULL, 0);
10000958:       80 7f 00 10     lwz     r3,16(r31)
1000095c:       38 80 00 00     li      r4,0
10000960:       38 a0 00 00     li      r5,0
10000964:       48 01 09 31     bl      10011294 <shmat@plt>
10000968:       7c 60 1b 78     mr      r0,r3
1000096c:       3d 20 10 01     lis     r9,4097
10000970:       90 09 11 d0     stw     r0,4560(r9)
if (errno != 0) {
10000974:       48 01 08 d1     bl      10011244 <__errno_location@plt>
10000978:       7c 60 1b 78     mr      r0,r3
1000097c:       7c 09 03 78     mr      r9,r0
10000980:       80 09 00 00     lwz     r0,0(r9)
10000984:       2f 80 00 00     cmpwi   cr7,r0,0
10000988:       41 9e 00 50     beq-    cr7,100009d8 <main+0x10c>

Specify Language

gcc -x c a-c-source-file.with-a-non-standard-extension -o test.out

Great for legacy projects where where the file extensions don't match with GCC's expectations, while less of a problem since many projects got their start with GCC, this still is an issue with long-running projects that years back, used some other compiler. This stays in effect for the following file on the command line.

List Include File Dependencies

There's a whole family of things around -M. These produce a rule that could be used in a make file, with the included files as dependencies.

gcc -M <file name>

This shows you all includes, even those on the system path. Useful if you're doing porting work or validating if your compiler is working as expected and getting the files from the right place. You'll see something like this for a basic hello world program

hello.o: hello.c /usr/include/stdio.h /usr/include/features.h \
/usr/include/sys/cdefs.h /usr/include/gnu/stubs.h \
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/include/stddef.h \
/usr/include/bits/types.h /usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h /usr/include/libio.h \
/usr/include/_G_config.h /usr/include/wchar.h /usr/include/bits/wchar.h \
/usr/include/gconv.h \
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/include/stdarg.h \
/usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h
gcc -MM <file name>  

Like -M, but no system files. Great to see if your project is configured and working as expected.

gcc -M -MG <file name>  

The prior -M and -MM commands will stop if header files can be located. The parameter -MG will just produce the dependency list with the missing file. Engineers that have projects that generate header files as part of the build find -MM very handy.

gcc -M -MT '<target>' <file name> 

By default, the target will be the <file name>.o This command will make the default the value of <target>.

If the command was

gcc -M -MT '$(target)' hello.c

You would see

$(target): hello.c 

Symbol Trace

gcc -Wl,-y,printf hello.c

This is very handy when you want to understand the linker is finding a definition of a symbol. Some projects have name collisions or link order dependencies. This lets you see precisely what the linker is doing.

Given a hello world program, you would see output like

/tmp/ccwZx5UV.o: reference to printf
/lib/libc.so.6: definition of printf

The reference is in a temporary file created during the compilation process. If you were linking several object files together explicitly, you would see the name of the object file where printf was referenced.

Saving temporary files

gcc -save-temps hello.c
Read the rest of this entry >>

Saturday, October 25, 2008

Change directory command - cd

cd  - Change Directory - change the current working directory to a specific Folder.

Examples

Move to the sybase folder
$ cd /usr/local/sybase
$ pwd
/usr/local/sybase


Change to another folder
$ cd /var/log
$ pwd
/var/log


Quickly get back
$ cd –
$ pwd
/usr/local/sybase


move up one folder
$ cd ..
$ pwd
/usr/local/


$ cd ../../..
will move three levels backward



Man Pages

Syntax 
      cd [Options] [Directory]

Key
    -P    Do not follow symbolic links
    -L    Follow symbolic links (default)
If directory is not given, the value of the HOME shell variable is used.

If the shell variable CDPATH exists, it is used as a search path.
If directory begins with a slash, CDPATH is not used.

If directory is `-', this will change to the previous directory location (equivalent to $OLDPWD ).

The return status is zero if the directory is successfully changed, non-zero otherwise.
Read the rest of this entry >>

cat - Display the contents of a file

cat (concatenate) is a command to display the contents of a file in bash itself. This commands is used to view the content of the file without using any editor.


Examples:

Display a file
$ cat myfile.txt
Concatenate two files:
$ cat file1.txt file2.txt > union.txt
If you need to combine two files but also eliminate duplicates, this can be done with sort unique:
$ sort -u file1.txt file2.txt > unique_union.txt
Put the contents of a file into a variable
$ my_variable=`cat $myfile.txt`





Man Pages


Display the contents of a file (concatenate)

Syntax
      cat [Options] [File]...

Concatenate FILE(s), or standard input, to standard output.

  -A, --show-all           equivalent to -vET

  -b, --number-nonblank    number nonblank output lines

  -e                       equivalent to -vE

  -E, --show-ends          display $ at end of each line

  -n, --number             number all output lines

  -s, --squeeze-blank      never more than one single blank line

  -t                       equivalent to -vT

  -T, --show-tabs          display TAB characters as ^I

  -u                       (ignored)

  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB

      --help               display this help and exit

      --version            output version information and exit

With no FILE, or when FILE is -, read standard input.

Read the rest of this entry >>

List Directories and files

 Ls is a command to list directories and files.This is similar to the dir command  in Dos.
  List information about file(s) and Directories



 Few Example :

$ ls
Cygwin.bat  bin       dev  home  proc  tmp  var
Cygwin.ico  cygdrive  etc  lib   srv   usr

$ ls -lh
total 17K
-rwxr-xr-x  1 FSK None   57 May 18 16:10 Cygwin.bat
-rw-r--r--  1 FSK None 6.9K May 18 16:10 Cygwin.ico
drwxr-xr-x  2 FSK None    0 May 18 15:18 bin
dr-xr-xr-x  1   0 root    0 Jan  1  1970 cygdrive
drwxr-xr-x  2 FSK None    0 May 18 15:56 dev
drwxr-xr-x 22 FSK None    0 May 18 15:16 etc
drwxr-xr-x  3 FSK None    0 May 18 16:10 home
drwxr-xr-x 38 FSK None    0 May 18 15:18 lib
dr-xr-xr-x  1 FSK None    0 Dec  1  2006 proc
drwxr-xr-x  3 FSK None    0 May 18 15:59 srv
drwxr-xr-x  2 FSK None    0 May 18 15:18 tmp
drwxr-xr-x 17 FSK None    0 May 18 15:18 usr
drwxr-xr-x  9 FSK None    0 May 18 15:18 var






Man Pages


ls

ls [options] [names]
List contents of directories. If no names are given, list the files in the current directory. With one or more names, list files contained in a directory name or that match a file name. names can include filename metacharacters. The options let you display a variety of information in different formats. The most useful options include -F, -R, -l, and -s. Some options don't make sense together (e.g., -u and -c).

Options

-1, --format=single-column
Print one entry per line of output.
-a, --all
List all files, including the normally hidden files whose names begin with a period.
-b, --escape
Display nonprinting characters in octal and alphabetic format.
-c, --time-ctime, --time=status
List files by status change time (not creation/modification time).
--color =when
Colorize the names of files depending on the type of file. Accepted values for when are never, always, or auto.
-d, --directory
Report only on the directory, not its contents.
-f
Print directory contents in exactly the order in which they are stored, without attempting to sort them.
--full-time
List times in full, rather than using the standard abbreviations.
-g
Long listing like -l, but don't show file owners.
-h
Print sizes in kilobytes and megabytes.
--help
Print a help message and then exit.
-i, --inode
List the inode for each file.
--indicator-style=none
Display filenames without the flags assigned by -p or -f (default).
-k, --kilobytes
If file sizes are being listed, print them in kilobytes. This option overrides the environment variable POSIXLY_CORRECT.
-l, --format=long, --format=verbose
Long format listing (includes permissions, owner, size, modification time, etc.).
-m, --format=commas
Merge the list into a comma-separated series of names.
-n, --numeric-uid-gid
Like -l, but use group ID and user ID numbers instead of owner and group names.
-o
Long listing like -l, but don't show group information.
-p, --filetype, --indicator-style=file-type
Mark directories by appending / to them.
-q, --hide-control-chars
Show nonprinting characters as ? (default for display to a terminal).
-r, --reverse
List files in reverse order (by name or by time).
-s, --size
Print file size in blocks.
--show-control-chars
Show nonprinting characters verbatim (default for printing to a file).
--si
Similar to -h, but uses powers of 1,000 instead of 1,024.
-t, --sort=time
Sort files according to modification time (newest first).
-u, --time=atime, --time=access, --time=use
Sort files according to file-access time.
--version
Print version information on standard output, then exit.
-x, --format=across, --format=horizontal
List files in rows going across the screen.
-v, --sort=version
Interpret the digits in names such as file.6 and file.6.1 as versions, and order filenames by version.
-w, --width=n
Format output to fit n columns.
-A, --almost-all
List all files, including the normally hidden files whose names begin with a period. Does not include the . and .. directories.
-B, --ignore-backups
Do not list files ending in ~ unless given as arguments.
-C, --format=vertical
List files in columns (the default format).
-D, --dired
List in a format suitable for Emacs dired mode.
-F, --classify, --indicator-style=classify
Flag filenames by appending / to directories, * to executable files, @ to symbolic links, | to FIFOs, and = to sockets.
-G, --no-group
In long format, do not display group name.
-H, --dereference-command-line
When symbolic links are given on the command line, follow the link and list information from the actual file.
-I, --ignore pattern
Do not list files whose names match the shell pattern pattern, unless they are given on the command line.
-L, --dereference
List the file or directory referenced by a symbolic link rather than the link itself.
-N, --literal
Display special graphic characters that appear in filenames.
-Q, --quote-name
Quote filenames with "; quote nongraphic characters.
-R, --recursive
List directories and their contents recursively.
-Rfile, --reload-state file
Load state from file before starting execution.
-S, --sort=size
Sort by file size, largest to smallest.
-U, sort=none
Do not sort files.
-X, sort=extension
Sort by file extension, then by filename.
Read the rest of this entry >>

 
Modified | Edited by Fasil