Bash Index : R - The 'R' Bash commands : description, flags and examples

mail

realpath

Usage

print the resolved path

Example

mail

reposync

Usage

Synchronize yum repositories to a local directory

Flags

Flag Default value Usage
-a arch --arch=arch current download packages for the arch architecture
-d --delete delete local packages no longer present in repository
--download-metadata download all the non-default metadata
-p destDir
--download_path=destDir
current dir download packages to destDir
-g --gpgcheck
  • Remove packages that fail GPG signature checking after downloading
  • exit status is 1 if at least one package was removed
-n --newest-only download only newest packages per-repo
-l --plugins enable yum plugin support
-r repoId
--repoid=repoId
all
  • query the repository identified as repoId
  • can be specified multiple times
-u --urls
  • list urls of what would be downloaded, don't download
  • can serve as a dry-run

Example

mail

rpm

Usage

The Red Hat Package Manager
The man differentiates :
  • a package name : myPackage
  • from a package file : myPackage-<version>-<arch>.rpm

Flags

Flag Usage
-e packageName
--erase packageName
erase (i.e. uninstall) packageName
-i packageName
--install packageName
  • install packageName
  • with --nodeps : skip dependencies check
-U packageName
--upgrade packageName
ugrade packageName = install new version + remove old version
-q [selectOptions] [queryOptions]
--query [selectOptions] [queryOptions]
query the RPM database, with :
  • selectOptions :
    • -a --all : query all installed packages
    • -f someFile --file someFile : display package owning file someFile
    • -p packageName --package packageName : query an (uninstalled) package
    • --whatrequires : list packages that require the specified package (source, opposite of rpm -qR)
  • queryOptions :
    • -l --list : list files of installed package
      see also : rpm -qi --filesbypkg packageName (source)
    • -R --requires : list packages required by the specified package (opposite of rpm -q --whatrequires)

Example

list files provided by a package
rpm -qlp packageFile
mail

reset

When the terminal is f*cked up because : you may recover it with :
reset
It's possible that what you type is not echo'ed to the screen, but it'll work anyway .
mail

return

Usage

return n

Since return outputs status codes (aka exit codes), it allows calling a function and processing its return value with && and ||.

Example

#!/usr/bin/env bash

returnParameter() { return $1; }

trueThenReturnParameter() { true; return $1; }

falseThenReturnParameter() { false; return $1; }

returnTrue() { true; return; }

returnFalse() { false; return; }

for testValue in 0 1 2 42 '' x; do
	returnParameter $testValue;          returnedValue1=$?
	trueThenReturnParameter $testValue;  returnedValue2=$?
	falseThenReturnParameter $testValue; returnedValue3=$?
	echo -e "test value : '$testValue'\treturned values : '$returnedValue1'\t'$returnedValue2'\t'$returnedValue3'\n"
done

echo
returnTrue && echo 'returned TRUE' || echo 'returned FALSE'
returnFalse && echo 'returned TRUE' || echo 'returned FALSE'
test value : '0'	returned values : '0'	'0'	'0'

test value : '1'	returned values : '1'	'1'	'1'

test value : '2'	returned values : '2'	'2'	'2'

test value : '42'	returned values : '42'	'42'	'42'

test value : ''		returned values : '0'	'0'	'1'

./test.sh: line 4: return: x: numeric argument required
./test.sh: line 9: return: x: numeric argument required
./test.sh: line 14: return: x: numeric argument required
test value : 'x'	returned values : '2'	'2'	'2'

returned TRUE
returned FALSE
mail

read

Usage

Read

Flags

Flag Usage
-a myArray "explode" substrings of the parameter into elements of myArray. Consider IFS to identify such substrings
-d delim the 1st character of delim is used to terminate the input line, rather than newline
-p prompt variableName An example is worth 1000 words :
read -p "What's your name ? " yourName; echo "Hello $yourName"
What's your name ? Bob
Hello Bob
-r raw input : disables interpretation of backslash escapes and line-continuation in the read data

Example

Read columns (source) :

This can be done by specifying several variables when calling read :
  • string='foo bar baz'; read a b c <<< "$string"; echo "$a $b $c"
  • text='one two three'; read -r col1 col2 col3 <<< "$text"; printf "col1: %s, col2: %s, col3: %s\n" "$col1" "$col2" "$col3"

Read data fields from a formatted text file :

tmpFile=$(mktemp tmp.XXXXXXXX); for i in {1..5}; do echo "${i}_1 ${i}_2 ${i}_3" >> "$tmpFile"; done; cat "$tmpFile"; while read itemA itemB itemC; do echo -e "$itemA\t$itemB\t$itemC"; done < "$tmpFile"; rm "$tmpFile"

Pop a group of lines from a file :

This will transpose this :
a
b
c

z
into this :
a	b	c
d	e	f
g	
y	z
tmpFile=$(mktemp tmp.XXXXXXXX); echo {a..z} | tr ' ' '\n' > "$tmpFile"; while true; do read a || break; read b; read c; echo -e "$a\t$b\t$c"; done < "$tmpFile"; rm "$tmpFile"
mail

runlevel

Display the previous and the current runlevels. If no previous runlevel is known, it will just display N.
On older systems (?), this information may be available in the RUNLEVEL and PREVLEVEL environment variables.
mail

resize2fs

Usage

resize2fs [options] device size
Resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system.
If size parameter is not specified, it will default to the size of the partition.

Flags

Flag Usage
-f force resize2fs to proceed with the filesystem resize operation, overriding some safety checks it normally enforces

Example

Shrink a partition :

resize2fs /dev/mapper/myLVM-homes 1800G
mail

renice

Usage

renice newNiceValue -p PID
Alter niceness (priority) of running processes. Instead of renice-ing a new process, it is wiser to nice it at startup.
Non-root users :
  • can only renice their own processes
  • can only increase the nice value (i.e. decrease priority) of a process (see example)

Flags

Flag Usage
-n newNiceValue
  • set the new nice value to newNiceValue
  • A niceness of −20 is the highest priority and 19 is the lowest priority. The default niceness a of process is inherited from its parent process, usually 0.
  • The -n is optional, but must be the 1st argument when used.
-p PID

Example

Read the nice value of a process :

Playing with renice :

sleep 3 & renice 5 -p $!
[1] 19145
19145 (process ID) old priority 0, new priority 5
sleep 3 & renice -n 5 -p $!
[1] 19167
19167 (process ID) old priority 0, new priority 5
Same as above since -n is optional.
sleep 3 & renice 5 -p $!; renice 5 -p $!
[1] 19208
19208 (process ID) old priority 0, new priority 5
19208 (process ID) old priority 5, new priority 5
You can not renice an already reniced process (or at least, not like this).
sleep 3 & renice +5 -p $!; renice +5 -p $!
[1] 19251
19251 (process ID) old priority 0, new priority 5
19251 (process ID) old priority 5, new priority 5
The + sign seems to make no big difference.
sleep 3 & renice 5 -p $!; renice 1 -p $!
[1] 19272
19272 (process ID) old priority 0, new priority 5
renice: failed to set priority for 19272 (process ID): Permission denied

  • The value we specify is NOT an increment, it's the expected value (see this example).
  • Only root can decrease a nice value (i.e. give higher priority to a process).

sleep 3 & renice 5 -p $!; renice 6 -p $!
[1] 20538
20538 (process ID) old priority 0, new priority 5
20538 (process ID) old priority 5, new priority 6
mail

Usage

readlink / readlink -f
regular file symlink
readlink (no result + causes an error) link target, either relative or absolute, depending on how the link was set
realpath

Example

cd /tmp; touch myFile; ln -s myFile linkRelative; ln -s /tmp/myFile linkAbsolute
for i in myFile linkRelative linkAbsolute; do
	a=$(ls -l "$i")
	b=$(readlink "$i")
	c=$(readlink -f "$i")
	d=$(realpath "$i")

	cat <<-EOF

		ls -l :       '$a'
		readlink :    '$b'
		readlink -f : '$c'
		realpath :    '$d'
		EOF
done
rm myFile linkRelative linkAbsolute
ls -l :       '-rw------- 1 bob users 0 août   6 15:31 myFile'
readlink :    ''
readlink -f : '/tmp/myFile'
realpath :    '/tmp/myFile'

ls -l :       'lrwxrwxrwx 1 bob users 6 août   6 15:31 linkRelative -> myFile'
readlink :    'myFile'
readlink -f : '/tmp/myFile'
realpath :    '/tmp/myFile'

ls -l :       'lrwxrwxrwx 1 bob users 6 août   6 15:31 linkAbsolute -> /tmp/myFile'
readlink :    '/tmp/myFile'
readlink -f : '/tmp/myFile'
realpath :    '/tmp/myFile'
mail

rm

Flags

Flag Usage
-i ask for confirmation before deleting each target file
-f force delete
-r delete recursively

Example

/bin/rm: Argument list too long (source) :

  • cd directory; find . -name '*' -type f | xargs rm
  • cd directory; find . -name '*' -type f -delete
mail

rename

Installed with the Debian package

/usr/bin/rename comes from the rename package
Actual rename executable file :
which rename
/usr/bin/rename
ll /usr/bin/rename
lrwxrwxrwx 1 root root 24 juil.  1 09:25 /usr/bin/rename -> /etc/alternatives/rename
ll /etc/alternatives/rename
lrwxrwxrwx 1 root root 20 juil.  1 09:25 /etc/alternatives/rename -> /usr/bin/file-rename
or even shorter :
realpath $(which rename)
/usr/bin/file-rename

Usage

Rename files
There are 2 flavors of rename :
  1. a version using a substitution RegExp (what this article is about, read below)
  2. a version invoked as :
    rename from to files
    which renames files by replacing the first occurrence of from with to.

Flags

Flag Usage
-n no action : show how files would have been renamed

Example

Replace all spaces by underscores in filenames :

rename 's/ /_/g' *

Replace some substrings while keeping others, using callbacks :

The regular expressions have a callback syntax to refer to a "previously matched substring". In other tools (sed, Emacs, Vi, ...), such callbacks are written \n (n being the number of the matched expression, starting at 1). Even though rename accepts this syntax, it will complain saying it prefers them specified using the $ sign : $n. Example :
touch /tmp/file_01.txt; rename 's/(^.*_)..(\.txt)/${1}02$2/' /tmp/file_01.txt; ls -l /tmp/file_*
  • Here, curly braces are used to refer to the 1st matched expression because the following characters are digits, and we don't want this reference to be interpreted as the 102nd matched expression.
  • rename prefers the $n syntax, but we mustn't forget that a $ sign within double quotes will be expanded by the shell interpreter (Bash or others).

Nice numbering of ordered files :

Let's consider the set of files produced by :
prefix='testFile_'; extension='.ext'; for i in {1..20}; do touch "$prefix$i$extension"; done
For the files 1 to 9 to be numbered 01 to 09, just run :
prefix='testFile_'; rename 's/_([0-9]\.)/_0$1/g' $prefix*
  • $prefix is used to avoid affecting all files with a rename regExp *
  • the substring to match for re-use must be enclosed by ( ) (no need to escape parens), and is recalled as $n (not \n), n being the nth matched substring
This hack _may_ turn useless with : {01..20}

Rename and number a collection of files :

Let's consider a collection of files :
  • file names are not consistent
  • extensions are not consistent either : mp3/MP3, jpg/jpeg/JPG, ...
  • files have no numbering
  • the relative numbers of files are not important
... in which we would like to decrease entropy. To do so, let's :
  • rename files based on a pattern
  • number files
Let's do it with :
prefix='myFile - '; extension='.jpg'; fileNb=1; for i in *; do rename "s/^.*$/$prefix$fileNb$extension/g" "$i"; fileNb=$((fileNb+1)); done; rename 's/ ([0-9]'$extension')/ 0$1/g' *
The rename after (not within) the for loop is to re-arrange file numbers nicely.

one-liners :

  • files downloaded from YouTube are named after artist - title - [youtubeVideoID].webm, and we'd like the [youtubeVideoID] part out (including brackets and leading space) :
    rename 's/ \[[^\]]+]//' *webm highlighted brackets are those above
  • add missing hyphen to change nn name.extension into nn - name.extension
    rename -n 's/^([0-9]{2} )/$1- /' *extension
  • remove suffix from filename with a trailing UUID / random suffix, such as description-of-whats-inside-XyZ123.extension
    rename 's/-[A-Za-z0-9]{6}\././' *extension
  • remove duplicate marker from filename : someFile(n).extension
    rename 's/\([0-9]\)\././' *extension
  • rename <32>_<8>.extension files into <8>.extension : like c11494a588c0d65b094a094a588c0d6588c0d6f8380a90c2ab1_f46f29ba.extension into f46f29ba.extension
    rename 's/^[0-9a-zA-Z]{32}_([0-9a-zA-Z]{8}\.[a-z]{3,4})/$1/' *extension
  • change filename case :
    touch MYFILE_123; rename 'y/A-Z/a-z/' *123; ls -1 *123; rename 'y/a-z/A-Z/' *123; ls -1 *123
    myfile_123
    MYFILE_123
    This works only on case-sensitive filesystems, where MYFILE and myfile are distinct files which can co-exist in a single directory (hack required otherwise , read below).
    If working on a FAT32 volume (aka vfat) where MYFILE and myfile are the same file, the idea would be to :
    1. rename MYFILE into myfile_someArbitrarySuffix
    2. then change myfile_someArbitrarySuffix into myfile
    Let's give this a try :
    1. initialize things :
      suffix='_lowercase'; touch MYFILE.EXTENSION; ll
    2. change filename case :
      • rename 'y/A-Z/a-z/; s/^(.*)$/$1'$suffix'/' * && rename 's/^(.*)'$suffix'$/$1/' *"$suffix"
      • rename "y/A-Z/a-z/; s/^(.*)\$/\$1$suffix/" * && rename "s/^(.*)$suffix\$/\$1/" *"$suffix"
    3. clean before leaving :
      ll; rm myfile.extension
    Both commands return:
    -rwxrwx--- 1 root 98 0 nov.   7 16:43 MYFILE.EXTENSION
    -rwxrwx--- 1 root 98 0 nov.   7 16:43 myfile.extension