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

mail

ffmpeg

Usage

Convert audio and video.
avconv replaces ffmpeg that is now obsolete.

This is actually wrong : due to a flamewar between ffmpeg and avconv project leaders, the avconv team started making things confusing for everybody with questionable assertions (sources : 1, 2). ffmpeg is still actively developed and maintained.

Flags

General flags (source) :

Flag Usage
-af filterGraph Apply filterGraph to the input audio stream
A filterGraph is a process made of several piped filters. One of the available filterGraphs is aformat
-f format Force input or output file format. The format is normally autodetected for input files and guessed from file extension for output files, so this option is not needed in most cases. To list supported file formats (containers) : ffmpeg -formats
  • D : container can be decoded (read)
  • E : container can be encoded (written)
-i inputFile input file. It is possible to have several input files :
ffmpeg -i file1 -i file2
and to refer to individual streams with -map x:y
-map fileId:streamId -map x:y refers to :
  • x : ID of input file (1st file listed on the command line with -i file is 0, and so on)
  • y : input file stream ID
Examples : 1, 2
-ss position
  • When used as an input option (before -i), seeks in this input file to position.
  • When used as an output option (before an output filename), decodes but discards input until the timestamps reach position.
  • Seeking is made as "best effort" (read documentation for details)
  • position may be either in seconds or in hh:mm:ss[.xxx] form.
-t duration Stop writing the output after its duration reaches duration.
duration may be a number in seconds, or in hh:mm:ss[.xxx] form.
-threads n Use n threads for processing.
Or auto : ??? (showed the maximum CPU usage when tried )
Or 0 : ???
-y Overwrite output files without asking (actually auto-answers "yes" to Overwrite (y/n) ? prompt.)

Audio / video / filter / preset flags :

Flag Usage
-aspect ratio Set the video display aspect ratio
  • It is possible to apply this to a given stream with -aspect:x:y ratio
  • The result is fine in VLC, my but my LG TV sees no difference
-c codec
-codec codec
Generic specification : -codec[:optional_stream_specifier] codec input|output|stream
Select a decoder (when used before an input file) or an encoder (when used before an output file) for one or more streams. codec is the name of a decoder/encoder or the special value copy (output only) to indicate that the stream is not to be re-encoded (often seen as : -c copy).
-acodec codec
-codec:a codec
Specify the audio codec.
-c:a copy means that the input audio will be copied as is, without any transcoding.
-b:stream value Set the bitrate of the (optional parameter) stream stream (a for audio, v for video) to value bits/s (value can use the k suffix)
-crf quality Select the quality for constant quality mode. It goes from 0 (lossless) upwards logarithmically. You'll probably want a value between 19 and 25 in most cases.
-deinterlace deinterlace the video stream
-preset type Configuration preset. This does some automatic settings based on the general type of the image (?). Sets the speed of the encoding, either "slow", "medium", or "fast". Slow should get you smaller file sizes with an obvious tradeoff.
-r:stream value Set the frame rate of the specified video stream stream to value frames/s
-s:stream WxH Declare the size (width*height) of the input stream, OR set frame size to WxH
-vcodec codec
-codec:v codec
Specify the video codec.
-vf filter Apply the filter filter to the input video

Example

Get information about ffmpeg capabilities :

List available codecs :
ffmpeg -codecs
V : video codec
A : audio codec
S : subtitle codec

Get information (codec, bitrate, ...) about a media file (video / sound / video + sound track(s) / subtitles / ...) :

ffmpeg -i file, and look at the Stream #x.y lines.

This command returns a failure exit code as ffmpeg expects an "output file" parameter (I found no better solution so far ). For this reason ("failure" exit code + output message sent to stderr rather than stdout), you have to redirect everything to stdout before processing it :

for a single file :
ffmpeg -i file 2>&1 | grep 'Stream'
for multiple files :
streamType='Audio'; for mediaFile in *; do echo "$mediaFile"; ffmpeg -i "$mediaFile" 2>&1 | grep -E "Stream.*$streamType"; done
mail

fallocate

Installed with the Debian package

util-linux

Usage

preallocate (or deallocate) space to a file : actually "reserve" blocks to a file but doesn't write the whole file, which will look empty.
Works only on the filesystems that support the fallocate(2) system call : ext4, btrfs, xfs and ocfs2

Files created with fallocate actually count in the used space :

  • workDir="/run/user/$UID"; df -h "$workDir"; echo; testFile="$workDir/test_1GiB"; fallocate -l 1g "$testFile"; ls -lh "$testFile"; du -h "$testFile"; echo; df -h "$workDir"; [ -f "$testFile" ] && rm "$testFile"
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           3,1G  164K  3,1G   1% /run/user/1000
    
    -rw------- 1 bob developers 1,0G déc.  19 12:37 /run/user/1000/test_1GiB
    1,0G    /run/user/1000/test_1GiB	actual file size
    
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           3,1G  1,1G  2,1G  33% /run/user/1000
  • Alternate that creates a sparse file (source, details) :
    workDir="/run/user/$UID"; df -h "$workDir"; echo; testFile="$workDir/test_1GiB"; truncate -s 1g "$testFile"; ls -lh "$testFile"; du -h "$testFile"; echo; df -h "$workDir"; [ -f "$testFile" ] && rm "$testFile"
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           3,1G  164K  3,1G   1% /run/user/1000
    
    -rw------- 1 bob developers 1,0G déc.  19 12:45 /run/user/1000/test_1GiB
    0       /run/user/1000/test_1GiB	actual file size
    
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           3,1G  164K  3,1G   1% /run/user/1000

Flags

Flag Usage
-l length
--length length
specify the length of the range
  • as a number of bytes
  • using suffices :
    • KiB, MiB, GiB, : powers of 1024
    • K, M, G, (or k, m, g, ) : "iB" is implied when omitted, so powers of 1024, as above
    • KB, MB, GB, : power of 1000

Example

Create a test file having a specific size :

fallocate -l 100m some/file

Create a big invisible file:

  1. open a new terminal to experiment : you'll kill it at the end
  2. destinationDir='/tmp'; bigFileSize='3G'; bigFile=$(mktemp --tmpdir="$destinationDir" .XXXXXXXXXXXXXXXX); df -h "$destinationDir"; fallocate -l "$bigFileSize" "$bigFile"; exec 4< "$bigFile"; rm "$bigFile"; df -h "$destinationDir"
    Filesystem                  Size  Used Avail Use% Mounted on
    /dev/mapper/vgxubuntu-root  914G   52G  816G   6% /		before
    Filesystem                  Size  Used Avail Use% Mounted on
    /dev/mapper/vgxubuntu-root  914G   55G  813G   7% /		after : +3GB used
  3. try to ls it :
    ll "$bigFile"
    ls: cannot access '/tmp/.fKpHFCCUVWyxfSi2': No such file or directory
    You can't ls it because it's marked as deleted, even though it's not actually deleted yet because there's a process still using it.
  4. list it with other means :
    lsof +L1 | grep "$destinationDir"
    
    bash      11107 kevin    4r   REG  252,1 3221225472     0 48758805 /tmp/.fKpHFCCUVWyxfSi2 (deleted)		the process holding $bigFile
    grep      15062 kevin    4r   REG  252,1 3221225472     0 48758805 /tmp/.fKpHFCCUVWyxfSi2 (deleted)		the grep right above
  5. now, let's delete —actually release— it :
    kill -1 11107
    (the terminal closes)
  6. in a new terminal :
    df -h '/tmp' must be the same dir as in step 2
    Filesystem                  Size  Used Avail Use% Mounted on
    /dev/mapper/vgxubuntu-root  914G   52G  816G   6% /		back to the "before" usage
mail

fusermount

Usage

mount / umount FUSE filesystems

Flags

Flag Usage
-u unmount
-z lazy unmount
mail

findmnt

Usage

List all mounted filesystems or search for a filesystem. This is a decent replacement for mount's listing mode.

Flags

Flag Usage
(none) list all mounted filesystems in a tree-like format
-n --noheadings don't print the header line
-o list,of,columns
--output list,of,columns
output only columns listed in list,of,columns
-t list
--types list
limit the set of printed filesystems to those matching list :
  • one or more comma-separated filesystem types :
    • ext4
    • ext4,tmpfs
  • it is possible to negate a whole list by prefixing it with no : findmnt -t noext4,tmpfs
mail

fold

Usage

wrap each input line to fit in specified width

Flags

Flag Usage
-w width --witdh=width use width columns instead of 80

Example

echo {a..z} | fold -w 10
a b c d e
f g h i j
k l m n o
p q r s t
u v w x y
z
mail

fg

Resume the specified job in the foreground, and make it the current job.
fg takes a job ID —available from jobs— not a PID.
If no job ID is specified, the shell's notion of the current job is used (usually the most recent one).
mail

fc

Usage

fc is a utility that can, like history :

Flags

Flag Usage
-e -l -n -l flags and options for the select mode (not detailed here so far)
-s search=replacement command This is the replay mode :
  • command :
    • defaults to the previous command, so anything as short as fc -s just replays the previous command
    • otherwise : replays the previous command starting with command (this reads the commands history from newest to oldest and picks the first match)
  • search=replacement : replace search with replacement in the selected command (either the previous or the one starting by ...(read above )), then replay the edited command
    Don't forget the = sign between search and replacement.
Looks like successful fc -s commands are not recorded in the history.

Example

Fix my poor typing :

There's a typo I make quite often : while trying to run script.sh, I type
.:script.sh
instead of :
./script.sh
fc can help here :
  1. alias '::'='fc -s ".:"="./"'
  2. So the next time I type
    .:script.sh
    I'll get
    bash: .:script.sh: command not found
  3. Then, using my new alias :
    ::
    fc fixes the typo and runs the command
    (normal output of script.sh)
mail

factor

Usage

factor x y z prints the prime factors of each specified integer.

Example

factor 8 17 42

8: 2 2 2
17: 17
42: 2 3 7
mail

file

Usage

Determine file type.

Flags

Flag Usage
(none) Simply output the file type :
  • ASCII text, UTF-8 Unicode text
  • POSIX shell script text executable, Korn shell script text executable
  • JPEG image data, JFIF standard 1.01
  • ...
This also adds with CRLF line terminators when testing text files made on Windows.
-i --mime Output mime type strings rather than the more traditional human readable ones. Thus it may say text/plain; charset=us-ascii rather than ASCII text.
mail

false

Do nothing, and fail
false only returns a Unix-failure exit code : non-zero.
mail

ftp

Flags

Command Usage
ftp host open an ftp session on the distant server host
Use either your personal login/password or anonymous/[Enter]
ftp -s:commands.txt automatically run the commands from commands.txt (works only with Windows FTP)
get fileName download fileName to current local directory
mget * download all the files of the remote directory to the local directory
put fileName upload fileName to current remote directory
mput * upload all the files of the local directory to the current remote directory
delete fileName delete the specified remote file
mdelete delete multiple files
prompt Toggle interactive prompting. Interactive prompting occurs during multiple file transfers to allow the user to selectively retrieve or store files. By default, prompting is turned on. If prompting is turned off, any mget or mput will transfer all files, and any mdelete will delete all files.
binary / ascii toggle binary / ascii file transfer mode :
  • ASCII : for pure text files ( = that can be handled by simple text editors)
  • binary : compiled or assembled code such as executables or data files.
The FTP protocol shrinks files before transmission, which is done through dedicated algorithms depending on the file type: either ASCII or binary. Actually, ASCII files have a higher compression rate than binaries. Use ASCII transfer only if you're sure that the content to be sent is an ASCII file, otherwise default to binary.
rename remoteFile1 remoteFile2 rename the remote file called remoteFile1 into remoteFile2
lcd localDirectoryName change to local directory localDirectoryName
! mkdir localDirectoryName create the local directory localDirectoryName
open re-open connection after time-out disconnect (still in ftp prompt: ftp> _ )

Example

Connect to a secure FTP (SFTP) server :

Use cURL

mail

fuser

Usage

Identify processes (PID) using specific files or sockets

Without privileges, fuser won't display anything if the corresponding PID is not yours.

Flags

Flag Usage
-a --all Show all files specified on the command line. By default, only files that are accessed by at least one process are shown.
-k -signal myFile Send signal signal to process(es) accessing myFile. If signal is omitted, SIGKILL is sent.
With -i : prompt before killing a process.
-m myFile List processes accessing files that are on the same filesystem than myFile. It also displays Frcem flag to identify the access type.
-v verbose

Example

Get the webserver's PID :

fuser 80/tcp

List processes using port 443 :

fuser -va 443/tcp

List processes accessing the /home partition :

fuser -va /home
mail

free

Usage

Display amount of free / used memory (by reading /proc/meminfo).

Flags

Flag Usage
-b / -k / -m / -g display amounts in bytes / kilobytes / megabytes / gigabytes
-t display a "total" line

Example

basic usage : free -m

		total	used	free	shared	buffers	cached
Mem:		16085	15884	200	0	347	12940
-/+ buffers/cache:	2596	13489
Swap:		1951	0	1951
  1. the 1st row (Mem:) is about physical memory utilization, including the amount of memory allocated to buffers and caches.
  2. the 2nd row (-/+ buffers/cache:) shows the amount of physical memory currently devoted to system buffers and cache. This is particularly meaningful with regard to application programs, as all data accessed from files on the system that are performed through the use of read() and write() system calls pass through this cache. This cache can greatly speed up access to data by reducing or eliminating the need to read from or write to the HDD or other disk.

Interpreting numbers :

  • 16085 MB of RAM are installed in the machine
  • 15884 MB of RAM are used, meaning there's something written there by someone for someone
  • 200 MB of RAM are free : there's nothing written there. This space is somehow wasted
  • 0 MB of RAM are shared. "Shared" memory is data or code that is common to several processes (i.e. : several instances of a shell console). This column is now obsolete
  • 347 MB of RAM are used for disk buffering
  • 12940 MB of RAM are used to cache data and make the system faster
  • used - buffers - cached = 15884 - 347 - 12940 = 2597 : amount of used RAM in the -/+ buffers/cache: row
  • used + free = 15884 + 200 = 2596 + 13489 = 16085 = total

buffers and caches :

  • buffer : a portion of memory that is set aside as a temporary holding place for data that is being sent to or received from an external device, such as a HDD, keyboard, printer or network.
  • cache : every time data is read from a file on disk, it's read into memory, and goes into the cache. After this, the kernel has the option to discard the cache.