≡ Menu

shell variable

How do I count and print particular character (say D or digit 7) in a string or variable under Bash UNIX / Linux shell?
[click to continue…]

How do I change the default home page with Lynx browser under UNIX or Linux operating system? How do I set the startup page to http://google.co.uk with Lynx?
[click to continue…]

How do I get the current date in Unix or Linux shell scripting and store it into a shell variable? How do I print the current date using Unix shell script?
[click to continue…]

HowTo: Get Current Time In Shell Script

How do I get the current server time in shell script on Linux or Unix-like operating systems? How do I store the current time in the shell variable and use in my scripts?
[click to continue…]

FreeBSD csh / tcsh: Export Shell Variable

Q. I'm using FreeBSD 7 with csh (tcsh) shell. How do I export shell variable under FreeBSD operating systems?
[click to continue…]

Bash Shell Loop Over Set of Files

How do I run shell loop over set of files stored in a current directory or specified directory?
[click to continue…]

Q. I need to find file permission and store the same to a shell variable. How do I find out the file permission without parsing ls -l output?

A. Use GNU stat command to display file or file system status. It has option to display output in specific format.

Display stat for /etc/passwd file

Type the following command:
$ stat /etc/passwd

  File: `/etc/passwd'
  Size: 1675            Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d      Inode: 7899368     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2007-12-05 08:20:31.000000000 +0530
Modify: 2007-12-04 23:36:50.000000000 +0530
Change: 2007-12-04 23:36:50.000000000 +0530

Print access rights in octal format:
$ stat -c %a /etc/passwd


Print access rights in human readable format, enter:
$ stat -c %A /etc/passwd


Store access rights in octal format to a shell variable:
$ VAR=$(stat -c %a /etc/passwd)
$ echo $VAR

Other valid format sequences for files

  • %b : Number of blocks allocated (see %B)
  • %B : The size in bytes of each block reported
  • %d : Device number in decimal
  • %C : SELinux security context
  • %D : Device number in hex
  • %f : Raw mode in hex
  • %F : File type
  • %g : Group ID of owner
  • %G : Group name of owner
  • %h : Number of hard links
  • %i : Inode number
  • %n : File name
  • %N : Quoted file name with dereference if symbolic link
  • %o : I/O block size
  • %s : Total size, in bytes
  • %t : Major device type in hex
  • %T : Minor device type in hex
  • %u : User ID of owner
  • %U : User name of owner
  • %x : Time of last access
  • %X : Time of last access as seconds since Epoch
  • %y : Time of last modification
  • %Y : Time of last modification as seconds since Epoch
  • %z : Time of last change
  • %Z : Time of last change as seconds since Epoch