The most dificult thing with Linux, is that, there is no solution that solves a problem for everyone. Here you will find a collection from solutions, that if it doesn't help you, will probably open a way to solve it.
usermod - Append the
user to supplementary groups
Repository - Adding new debian
repositories
aptitude - Find out a
package name.
dpkg - Check if a
package is installed, manage .deb package files
apt-get - Install new
packages
split & cat - How to split large file into several smaller files using
split and
cat.
scp - Copy files with
Secure Shell
ssh -
Secure Shell without using passwords
sshfs - Mount a remote folder with
Secure Shell
tar - Transport
files easily.
USB Disks - Identify and
mount USB Disks.
NTFS Read/Write -
Mount Read/Write NTFS partitions.
gcc - Installing a
programming environment.
32bit in 64bit - Running 32Bit
programs in a 64Bit machine.
Medibuntu - Add Medibuntu repository for free and non-free packages like
Acrobat reader,
quick time player,…
Openoffice - update Openoffice in Ubuntu jaunty 9.04
ldd - check library dependences
ifconfig - Finding out the local TCP/IP adress.
uname - Finding out the machine architecture x86_64,i686…
issue - Finding out which linux distribution is running.
cpuinfo - Finding out CPU information.
graphic card - Finding out which graphic card is installed.
bashrc - Create a user specific configuration for non-login shells.
shell - Finding out which shell you are using.
Shell Script - Executing a shell script.
Shell script functions - Programming Shell scripts - functions.
Shell variables - Programming Shell scripts - variables.
Shell arrays - Programming Shell scripts - arrays.
Shell arrays2 - Programming Shell scripts - accessing array elements using index.
Shell tests - Programming Shell scripts - File test operations.
renamefiles - Shell scripts to rename all files in the current directory and subdirectories.
usermod |
|
Modify a user groups.
To append the user to supplementary groups run the command line
usermod -a GROUP userLogin
or edit the file
/etc/group.
Repository |
|
Adding new debian repositories.
A standard repository list exists at /etc/apt/sources.list. You can edit it as super user using an editor like nano, vi or your favorite one. Just paste the indicated repository address given from your new distributor. It will be something like (for a bluetooth-manager application):
deb http://download.tuxfamily.org/blueman hardy blueman
It is possible to find
deb addresses for application and
deb-src for source codes designed for local compilations.
Attention A not well known repository can damage your system when updating it.
When updating your system you will be alerted. The distributor also offers a gpg key to be added in your system. You can do it using 'wget' application and 'apt-key' as super user .
wget -q http://download.tuxfamily.org/blueman/blueman.gpg -O- | sudo apt-key add -
aptitude |
|
Find out a package name.
if you need to search for a package:
aptitude search <what-you-need>
<what-you-need> is the file name or part of it that you are looking for. For example, compiling some C++ code I have the following situation:
/usr/bin/ld: cannot find -lgobject-2.0
collect2: ld returned 1 exit status
make: *** [painterpaths] Error 1
So, the solution was to call aptitude and than install the missing package with apt-get.
$ aptitude search gobject
p gobject-introspection - Generate interface introspection data for GObject libraries
p gobject-introspection-glib-2.0 - Introspection data for GLib, GObject, Gio and GModule
p gobject-introspection-repository - Introspection data for several GNOME related libraries
$ sudo apt-get install gobject-introspection-glib-2.0
dpkg |
|
It is the base of the Debian package management system and is used to install, remove, and provide information about .deb packages.
Consult if a package is installed
Install a .deb package file.
apt-get |
|
Installing debian pakages.
sudo apt-get install package
To install the Secure Shell package:
split & cat |
|
How to split large file into several smaller files using split and cat.
Split the file my_file.tar in several files with 128MB with the prefix name split_file_
$ split --bytes=128m ./my_file.tar ./split_file_
The new files will be like
split_file_aa,
split_file_ab,
split_file_ac…
To concatenate it back use:
$ cat split_file_* > my_original_file.tar
cat will join the pieces into the new file
my_original_file.tar.
scp |
|
Copy files between machines using a secure connection.
scp file login@remote:path
For example, copying the file '~/class.h' to the remote computer 'm105' in the '~/source/' path from 'correia'.
scp ~/class.h correia@m105:~/source/
ssh |
|
Connect other machines with ssh without typing any password.
Check if the pakage Secure Shell (ssh) is installed, see dpkg. If necessary install it, see apt-get.
On the client side run the following commands:
Two files will be created. One has the private key (~/.ssh/id_rsa) and the other the public key (~/.ssh/id_rsa.pub).
Copy the public key to the server. You can use the
scp to do it.
On the server side:
cat id_rsa.pub >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys
Returning to the client side, is possible to test the connection.
Try just
if it still asking a password, so go ahead with this report.
Try to connect showing where the key file is saved.
ssh -i ~/.ssh/id_rsa server
Create the file ~/.ssh/config to use ssh without having to specify the private key file. Example of a config file:
Host server
IdentifyFile ~/.ssh/id_rsa
Now is possible to use ssh with a simplified command and without password.
This procedure generates a passwordless key. The remote system security is downgraded to the local security polices. Therefore it is also
considered a securety hole.
sshfs |
|
Mapping a network drive with ssh.
On the client side:
Check if the pakage 'sshfs' is installed, see dpkg. If necessary install it, see apt-get.
Give permission to a user to mount remote folders, use usermod to do it.
Be sure that the files '/dev/fuse' and '/usr/bin/fusermount' can be accessed from users in the group 'fuse'.
ls -l /dev/fuse /usr/bin/fusermount
change the group with the command chgrp. Example changing the group from /dev/fuse.
sudo chgrp fuse /dev/fuse
Create a new folder to mount to.
To mount use 'sshfs user@remote:path localFolder'. Example with user login name 'correia', remote computer 'm105', remote folder '/home/correia/'
and local folder 'remoteComputer'.
sshfs correia@m105:/home/correia/ ./remoteComputer
Secure Shell will ask for a password. Is possible to configure it to connect using key files, see
ssh to see how to do it.
To unmount use
fusermount -u ./remoteComputer
tar |
|
Compressing and expanding files with tar.
To Compress
tar -czf FILE.tgz directory
instead of directory is possible to write files separated by an empty character.
To expand the file
USB Disks |
|
Identify and mount USB Disks.
List the partition tables using 'fdisk':
$fdisk -l
Disk /dev/sdc: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdc1 1 2550 20482843+ c W95 FAT32 (LBA)
/dev/sdc2 * 2551 38913 292085797+ 7 HPFS/NTFS
In my case only the FAT Partition is mounted by the Operating System. To mount the NTFS Partition I use the 'mount' program as root.
sudo mount -t ntfs-3g /dev/sdc2 /home/correia/USBDisk
To unmount, also as root
sudo umount /home/correia/USBDisk
NTFS Read/Write |
|
Mount Read/Write NTFS partitions.
Help taken from http://www.swerdna.net.au/linhowtontfs.html . There you will find deep explanation from all steps below.
- Check/install ntfs-3g, libfuse2 and fuse
- GUI application ntfs-config turns write permission on and off, one-click. See qualification below.
- External USB NTFS drives will not automount rw for users when powered on, in USB Disks a brief example how to identify the partition tables.
- fstab for Windows-like permissions (drwxrwxrwx):
/dev/sda2 /mnt/winxp ntfs-3g defaults 0 0
- CLI temporary mount for Windows-like permissions (drwxrwxrwx):
mount -t ntfs-3g /dev/sda2 /mnt/winxp
- fstab for Linux-like permissions (drwxr-xr-x):
/dev/sda2 /mnt/winxp ntfs-3g uid=1000,gid=100,umask=0022 0 0
- CLI temporary mount for Linux-like permissions (drwxr-xr-x):
mount -t ntfs-3g -o rw,uid=1000,gid=100,umask=0022 /dev/sda3 /mnt/sda3
gcc |
|
Installing a programming environment.
Compiler package:
sudo apt-get install build-essential
The kernel headers in some cases are installed together with the gcc and will be installed after installing the basics libraries. Trying to install the package linux-kernel-headers at kubuntu 8.04 I got the following message:
$ sudo apt-get install linux-kernel-headers
Reading package list... Done
Building dependency tree
Reading state informatio... Done
Package linux-kernel-headers is a virtual package provided by:
linux-libc-dev 2.6.24-19.36
You should explicitly select one tho install.
E: Package linux-kernel-headers has no installation candidate
In other linux distributions I have explicitly packages for different kernel versions. So you can identify the kernel using the command 'uname'.
$ uname -a
Linux tlk60 2.6.24-19-generic #1 SMP Wed Jun 18 14:15:37 UTC 2008 x86_64 GNU/Linux
$ uname -r
2.6.24-19-generic
Use the command line 'apt-get' like above with the correct package name, in my case
$ sudo apt-get install linux-headers-2.6.24-19-generic
an elegant way is to type the command line in cascade ;), see below, but at the end is the same result.
$ sudo apt-get install linux-headers-`uname -r`
32bit in 64bit |
|
Running 32Bit programs in a 64Bit machine.
If you have problems to run 32 bit applications / libraries in a 64 bit machine. First thing is to verify the library dependencies, you can do it with "ldd <application>". So you can see if all libraries are available. If you need the 32 bit libraries, add it with:
sudo apt-get install ia32-libs
If saw a report at
http://www.linux.com/feature/142075 that tells about a possible problem with asound library "you get an error about an asound library being uninstallable", solution was to call the command
sudo apt-get install libasound2=1.0.15-3ubuntu4.
Medibuntu |
|
Add Medibuntu repository for free and non-free packages like Acrobat reader, quick time player,…
Medibuntu repository offers application like Acrobat reader, quick time player, windows specific video/audio codecs, DVD support and so on, to be installed easily with a command apt-get. It helps me a lot after an hour trying to install acroread.
The steps are done as follow:
- Check your Ubuntu distribution name. In my case is jaunty
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.04
DISTRIB_CODENAME=jaunty
DISTRIB_DESCRIPTION="Ubuntu 9.04"
- Download the source list from Medibuntu using the correct distribution name. Than update your system.
$ sudo wget http://www.medibuntu.org/sources.list.d/jaunty.list --output-document=/etc/apt/sources.list.d/medibuntu.list
$ sudo apt-get update && sudo apt-get install medibuntu-keyring && sudo apt-get update
For example to install Acrobat Reader just call now
sudo apt-get install acroread.
Another interesting package can be the support for Windows codecs
sudo apt-get install w32codecs, for 64bit machine use the package
w64codecs. Thanks to
http://thedaneshproject.com/posts/enable-medibuntu-in-ubuntu-810/ .
Openoffice |
|
Update Openoffice in Ubuntu jaunty 9.04
Add the OpenOffice.org source address over the system menu "System->Administration->Software Sources". Go to the tab "Third-Party Software" and add two new adresses as shown below. You can also edit the file /etc/apt/sources.list in your favourite editor.
deb http://ppa.launchpad.net/openoffice-pkgs/ubuntu jaunty main
deb-src http://ppa.launchpad.net/openoffice-pkgs/ppa/ubuntu jaunty main
Than close the window. At the moment no Update can be started. A new key must be added using the apt-key command line as below:
$ sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 60D11217247D1CFF
Now is possible to update and upgrade the system using apt
$ sudo aptitude update
$ sudo aptitude upgrade
ldd - check library dependences
ldd |
|
check library dependencies.
With the command line below is possible to see if a program or library has it dependencies resolved. In this example the library 'libNet.so' are checked. the result shows three dependencies and all necessary files were found.
$ ldd libNet.so
linux-vdso.so.1 => (0x00007fffdbffe000)
libQtCore.so.4 => /usr/lib/libQtCore.so.4 (0x00007f1dbc094000)
libz.so.1 => /usr/lib/libz.so.1 (0x00007f1dbbe7d000)
I rename the file libQtCore.so.4 and call the ldd again. Obviously the dependencies are not resolved and the libNet.so will not work.
$ ldd libQtNet.so
linux-vdso.so.1 => (0x00007fff389fe000)
libQtCore.so.4 => not found
libz.so.1 => /usr/lib/libz.so.1 (0x00007f6c8180d000)
Below a small Shell script to validade a file
#!/bin/sh
# Detects library dependencies
DEP=`ldd $1 | grep "not found" | cut -d\ -f1`
noDEP=`ldd $1 | grep "not found" | wc -l`
if [ ${noDEP} -gt 1 ]; then
DEPSTR="There are ${noDEP} dependencies not resolved.\n ${DEP}"
elif [ ${noDEP} -eq 1 ]; then
DEPSTR="There is ${noDEP} dependence not resolved.\n ${DEP}"
else
DEPSTR="All dependencies are resolved.\n"
fi
echo ${DEPSTR}
The script is saved with the name 'testDependencies' and running the script we have
$ ./testDependencies libQtNet.so
There is 1 dependence not resolved.
libQtCore.so.4
ifconfig |
|
Finding out the local network configuration.
With the command line below is possible to see the local network configuration, like TCP/IP and MAC adress.
uname |
|
Finding out the machine architecture, x86_64, i686…
I have some problems with shell scripts that use 'uname -p' to identify the architecture. The solution is to not use '-p', but '-m'.
I found a good example at http://mule.mulesource.org/jira/browse/MULE-1288 that shows:
uname -a on FC6:
Linux hostname.com 2.6.18-1.2849.fc6 #1 SMP Fri Nov 10 12:34:46 EST 2006 x86_64 x86_64 x86_64 GNU/Linux
uname -a on Gentoo:
Linux hostname.com 2.6.19-gentoo-r2 #1 SMP Wed Dec 13 08:27:21 CST 2006 x86_64 Intel(R) Core(TM)2 CPU T7200 @ 2.00GHz GenuineIntel GNU/Linux
Here's the breakup:
FC6:
uname -m = x86_64
uname -p = x86_64
uname -i = x86_64
Gentoo:
uname -m = x86_64
uname -p = Intel(R) Core(TM)2 CPU T7200 @ 2.00GHz
uname -i = GenuineIntel
issue |
|
Finding out which linux distribution is running.
There are some ways to do it.
- The simple answer can be reached reading the file issue.
$ cat /etc/issue
Ubuntu 7.04 \n \l
The other options will give more information about the Operating System.
- From the Boot Time messages:
$ dmesg | head -1
[ 0.000000] Linux version 2.6.20-16-generic (root@terranova) (gcc version 4.1.2 (Ubuntu
4.1.2-0ubuntu4)) #2 SMP Tue Feb 12 05:41:34 UTC 2008 (Ubuntu 2.6.20-16.35-generic)
- Reading the 'version' file:
$ cat /proc/version
Linux version 2.6.20-16-generic (root@terranova) (gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4))
#2 SMP Tue Feb 12 05:41:34 UTC 2008
or
$ ls -d /etc/*release /etc/*version
/etc/debian_version /etc/lsb-release
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=7.04
DISTRIB_CODENAME=feisty
DISTRIB_DESCRIPTION="Ubuntu 7.04"
$ cat /etc/debian_version
4.0
cpuinfo |
|
Finding out CPU information.
How many processors, type, speed, cache size and so on ara available in the file cpuinfo.
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 2
model name : Intel(R) Pentium(R) 4 CPU 2.80GHz
stepping : 5
cpu MHz : 2813.643
cache size : 512 KB
...
graphic card |
|
Finding out which graphic card is installed.
Some commands can only be used as root. But I am happy that also as user is possible to read the graphic card name.
$ glxinfo | grep renderer
OpenGL renderer string: Quadro 4000/PCIe/SSE2
With some patience it is possible to to find in XFree86.0.log oder Xorg.0.log the graphic card name. Xorg will show also the monitor name, GPU RAM type, monitor resolutions and more.
$ cat /var/log/Xorg.0.log | grep GPU | more
...
[ 112.048] (II) NVIDIA(GPU-0): Display (DELL U2410 (DFP-2)) does not support NVIDIA 3D Vision
[ 112.049] (II) NVIDIA(0): NVIDIA GPU Quadro 4000 (GF100GL) at PCI:3:0:0 (GPU-0)
[ 112.049] (II) NVIDIA(0): GPU RAM Type: GDDR5
...
The other commands that I found are:
cat /var/log/XFree86.0.log | grep "GPU detected"
/sbin/lspci | grep VGA
cat /proc/pci | grep VGA
bashrc |
|
Create a user specific configuration for non-login shells.
bashrc are placed on the user root path and is executed automatically by bash for non-login shells.
I have created a directory called .profile.user and let the bashrc search the bash scripts inside this directory. I append in my bashrc the following script:
# set user environment
if [ -d ${HOME}/.profile.user ]; then
for i in ${HOME}/.profile.user/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
In
.profile.user I have files like
OpenFOAM.sh
#!/bin/sh
# CFD OpenFOAM environment variable settings.
# for cfd users OpenFOAM environment will be added
CFDUSER=`id | grep -i "cfd"`
if [ -n "${CFDUSER}" ];then
# if ! id | grep -i "cfd"; then
echo "OpenFOAM environment variable was added."
export FOAM_VERSION=1.5
export FOAM_INST_DIR=/opt/CFD/OpenFOAM
foamDotFile=${FOAM_INST_DIR}/OpenFOAM-${FOAM_VERSION}/etc/bashrc
[ -f $foamDotFile ] && . $foamDotFile
fi
Fluent.sh
#!/bin/sh
# CFD Fluent environment variable settings.
# for cfd users Fluent environment will be added
CFDUSER=`id | grep -i "cfd"`
if [ -n "${CFDUSER}" ];then
# if ! id | grep -i "cfd"; then
echo "Fluent environment variable was added."
echo " > Available softwares: fluent and gambit"
export PATH=/opt/CFD/Fluent.Inc/bin:$PATH
fi
Qt library
#!/bin/sh
# Qt 4.5.1 environment variable settings.
DEVELUSER=`id | grep -i "developer"`
if [ -n "${DEVELUSER}" ];then
# if ! id | grep -i "cfd"; then
echo "Qt 4.5.1 environment variable was added."
PATH=/usr/local/share/qtsdk-2009.02/qt/bin:"${PATH}"
fi
and
Ansys.sh
#!/bin/sh
# FEM/CFD Ansys.Inc environment variable settings.
# for cfd users Ansys, CFX and Fluent v12 environment will be added
CFDUSER=`id | grep -i "cfd"`
if [ -n "${CFDUSER}" ];then
# if ! id | grep -i "cfd"; then
echo "Ansys.Inc environment variable was added."
echo " > Available softwares: cfx5, fluent120 and ansys120"
export LM_LICENSE_FILE=$LM_LICENSE_FILE:1055@134.169.9.120
export PATH=/opt/CFD/ansys_inc/v120/ansys/bin:/opt/CFD/ansys_inc/v120/fluent/bin:/opt/CFD/ansys_inc/v120/CFX/bin:$PATH
fi
shell |
|
Finding out which shell you are using.
The environment variable SHELL held informaiton about the shell. Use 'echo' to display its value.
or
Other possibility is looking at the running processes
Well known shells are:
bash |
Bourne shell |
csh |
C shell |
tcsh |
TC shell |
ksh |
Korn shell |
dash |
Debian-Almquist shell |
sh |
Oft a link to bash or dash |
Shell Script |
|
Executing a shell script.
First of all, make sure that the script has execute permissions for the correct owners in order to be runnable. Use chmod and chown to setup the permitions.
There are several ways to run a script (the script file will be known here as 'shellscript'):
but if the current path isn't included into the system Path you need to specify it by calling the script as follows:
- specifiing the shell (example with bash)
ATTENTION: Both ways will start a
subshell, execute all instrucitons from the script and close the subshell. In other words, the variables, functions and aliases created in this subshell are only known into the particular bash session of that subshell. When that shell exits and the parent regains control, everything is cleaned up and all changes to the state of the shell made by the script, are forgotten. This is the most common way to execute a script and also strong recommended to keep the user environment free from specific script variables or functions.
- If you intend to change the user environment and therefore start the script in the current shell, do as follows:
The Bash 'source' built-in is a synonym for the Bourne shell '.' (dot) command.
A good shell script reference is "Bash Guide for Beginners", http://www.tldp.org/LDP/Bash-Beginners-Guide/html , from Machtelt Garrels.
Shell script functions |
|
Programming Shell scripts - functions.
Bellow an example of a function in a Shell script that read files in a predefined directory (~/projects), formatting the output in one column.
#!/bin/bash
WORKSPACE="~/projects"
function GetProjectList {
LSDIR=`ls -C1 $WORKSPACE`
}
GetProjectList
echo "My Workspace is build with:"
echo $LSDIR
Shell variables |
|
Programming Shell scripts - variables.
Special variables in a Shell:
$_ |
last used command line |
$0 |
running shell |
$1, $2,.. |
first argument, second … |
$#argv |
number of parameters |
$! |
the process nummer of the last started aplication in background |
$? |
return the Return-Code of the last running kommand (0 for finished without error) |
- Local variables are created by the keyword local.
#!/bin/bash
HELLO=Hello
function hello {
local HELLO=World
echo $HELLO
}
echo $HELLO
hello
echo $HELLO
Another example at this time in tcshell. In this example a given string will be searched into myFile.txt.
#!/bin/tcsh
grep $1 myFile.txt
if ( $? == 0 ) then
echo "string $1 found in myFile.txt"
else
echo "string $1 not found"
endif
Shell arrays |
|
Programming Shell scripts - arrays.
Arrays can be vary useful in Shell scripts. Below a short example how to create and operate with arrays. A vary good reference can be found at http://tldp.org/LDP/abs/html/ .
#!/bin/bash
WORKSPACE="~/projects"
LSDIR=`ls -d $WORKSPACE/*`
PROJECTS=( $LSDIR )
for proj in ${PROJECTS[@]}; do
echo "Configuring project $proj ..."
done
The following operations can be done with arrays
${PROJECTS[@]} |
all elements |
${PROJECTS[*]} |
all elements |
${PROJECTS[0]} |
first element |
${PROJECTS[1]} |
second element |
${#PROJECTS[@]} |
array size |
${PROJECTS{@}:4} |
all elements from (4) til end |
${PROJECTS{@}:4:7} |
all elements from (4) til (7) |
${PROJECTS{@}#*Tester} |
all elements but remove the elements ending with 'Tester' |
${PROJECTS{@}%*Tester} |
all elements but remove in each element the ending text 'Tester' if it matches |
${PROJECTS{@}/Tester/BAK} |
all elements replacing in each element 'Tester' with 'BAK' |
${PROJECTS{@}/#Tester/BAK} |
all elements replacing the front-end occurrences of sub-string 'Tester' with 'BAK' |
${PROJECTS{@}/%Tester/BAK} |
all elements replacing the back-end occurrences of sub-string 'Tester' with 'BAK' |
Shell arrays 2 |
|
Programming Shell scripts - accessing array elements using index.
Writing arrays and accessing its elements using an index variable.
Important in this script are the functions to compute the array size, (${#array[*]}, see also Shell arrays), and the increment for the while loop, let i++.
#!/bin/bash
array=("wert 1" "wert 2" "wert 3" "wert 4")
array[2]="wert C"
echo ${array[*]}
len=${#array[*]}
i=0
while [ $i -lt $len ]; do
echo "$i: ${array[$i]}"
let i++
done
exit 0
Shell tests |
|
Programming Shell scripts - File test operations.
An short example can be:
#!/bin/bash
MYFILE=~/testFile.txt
if [ -f $MYFILE ]; then
echo "file exists"
else
echo "file does not exists"
fi
#!/bin/tcsh
MYFILE=~/testFile.txt
if ( -f ${MYFILE} ) then
echo "file exists"
else
echo "file does not exists"
endif
Working with
files. Returns true if…
-e |
file exists |
-a |
file exists. This is identical in effect to -e. It has been "deprecated," [1] and its use is discouraged. |
-f |
file is a regular file (not a directory or device file) |
-s |
file is not zero size |
-d |
file is a directory |
-b |
file is a block device (floppy, cdrom, etc.) |
-c |
file is a character device (keyboard, modem, sound card, etc.) |
-p |
file is a pipe |
-h |
file is a symbolic link |
-L |
file is a symbolic link |
-S |
file is a socket |
-t |
file (descriptor) is associated with a terminal device. This test option may be used to check whether the stdin ([ -t 0 ]) or stdout ([ -t 1 ]) in a given script is a terminal. |
-r |
file has read permission (for the user running the test) |
-w |
file has write permission (for the user running the test) |
-x |
file has execute permission (for the user running the test) |
-g |
set-group-id (sgid) flag set on file or directory. If a directory has the sgid flag set, then a file created within that directory belongs to the group that owns the directory, not necessarily to the group of the user who created the file. This may be useful for a directory shared by a workgroup. |
-u |
set-user-id (suid) flag set on file. A binary owned by root with set-user-id flag set runs with root privileges, even when an ordinary user invokes it. [2] This is useful for executables (such as pppd and cdrecord) that need to access system hardware. Lacking the suid flag, these binaries could not be invoked by a non-root user. "-rwsr-xr-t 1 root 178236 Oct 2 2000 /usr/sbin/pppd" A file with the suid flag set shows an s in its permissions. |
-k |
sticky bit set. Commonly known as the sticky bit, the save-text-mode flag is a special type of file permission. If a file has this flag set, that file will be kept in cache memory, for quicker access. [3] If set on a directory, it restricts write permission. Setting the sticky bit adds a t to the permissions on the file or directory listing. "drwxrwxrwt 7 root 1024 May 19 21:26 tmp/" If a user does not own a directory that has the sticky bit set, but has write permission in that directory, she can only delete those files that she owns in it. This keeps users from inadvertently overwriting or deleting each other's files in a publicly accessible directory, such as /tmp. (The owner of the directory or root can, of course, delete or rename files there.) |
-O |
you are owner of file |
-G |
group-id of file same as yours |
-N |
file modified since it was last read |
f1 -nt f2 |
file f1 is newer than f2 |
f1 -ot f2 |
file f1 is older than f2 |
f1 -ef f2 |
files f1 and f2 are hard links to the same file |
! |
"not" — reverses the sense of the tests above (returns true if condition absent). |
Working with
strings. Returns true if…
-z |
the length of string is zero. |
-n |
the length of string is non-zero. |
string1 == string2 |
the strings are equal. |
string1 != string2 |
the strings are not equal. |
string1 < string2 |
string1 sorts before string2 lexicographically in the current locale. |
string1 > string2 |
string1 sorts after string2 lexicographically in the current locale. |
Working with
integers. Return true if…
arg1 -eq arg2 |
arg1 is equal to arg2 |
arg1 -ne arg2 |
arg1 is not equal to arg2 |
arg1 -lt arg2 |
arg1 is less than arg2 |
arg1 -le arg2 |
arg1 is less than or equal to arg2 |
arg1 -gt arg2 |
arg1 is greater than arg2 |
arg1 -ge arg2 |
arg1 is greater or equal to arg2 |
renamefiles |
|
Shell scripts to rename all files in the current directory and subdirectories.
Usage: renamefiles [OPTION] SEARCH DEST
Rename all SEARCH files to DEST in local directory and upper directories.
Mandatory arguments to long options are mandatory for short options too.
-R, —recursive work subdirectories recursively.
—help display this help and exit
Example:
renamefiles U30 U03
original File : VW216_U30_K04
after renamefiles: VW216_U03_K04
#!/bin/bash
if [ "$1" == "--help" ] || [ "$#" -eq 0 ]
then
echo "Usage: renamefiles [OPTION] SEARCH DEST"
echo "Rename all SEARCH files to DEST in local directory and upper directories."
echo ""
echo "Mandatory arguments to long options are mandatory for short options too."
echo " -R, --recursive work subdirectories recursively. "
echo " --help display this help and exit"
echo ""
echo "Example:"
echo " renamefiles U30 U03"
echo " original File : VW216_U30_K04"
echo " after renamefiles: VW216_U03_K04"
echo ""
#rename files recursively
elif [ "$1" == "-R" ] || [ "$1" == "--recursive" ]
then
for file in $(find . -iname "*$2*")
do
mv $file $(echo "$file" | sed -r 's|'$2'|'$3'|g')
done
else #rename files in local directory
for file in $(ls *$1*)
do
mv $file $(echo "$file" | sed -r 's|'$1'|'$2'|g')
done
fi