Table of Contents

Linux - File

About

Linux file management

Type

hidden

A full stop '.' before the name of the file makes it a hidden file. Example '.bash'

Sh - Hidden files

Metadata

See Linux - Stat (File status)

Management

Get

Filename and extension

Using Parameters Expansion Removal

fileFullName=$(basename "$filePath")
# or
fileFullName="${filePath##*/}"
extension="${fileFullName##*.}"
filename="${fileFullName%.*}"

parent directory

VAR=/filedoesnt/exist
echo ${VAR%/*}
/filedoesnt

dirname "dir/file"
dir

File Permission

Linux - File/Folder Permissions - Access Control List ( ACL ) - Posix Model

File exists

if [ ! -f $PATH ]; then
  echo "File $PATH not found!"
fi
# short cut for the test command
if ! test -f $PATH; then
  echo "File $PATH not found!"
fi

Absolute path

ABSOLUTE_PATH=$(realpath "${FILE_NAME}")

Size

with Linux - ls (List directory content)

ls -lh myFile | awk '{ print $5 }'

where:

Monitoring the size of a (log) file

Example:

# the program that run
program=unzip
# the log of the progam
logfile='restoreAll.log'
# Don't touch below, the program
count=0
while kill -0 $(pgrep ${program}) 2> /dev/null; do
    count=$(( $count + 1 ))
	size=$(ls -lh ${logfile} | awk '{ print $5 }')
    echo "${count} - Process is running and the log is ${size}"
    sleep 10
done
echo "${count} - Process has exited"

Comparison

cmp --silent $old $new || echo "files are different"
cmp -b $old $new 

See also : diff

Diff

diff file1 file2 | cat -t

where:

See also: comparison

Filter a list of files ?

This command below gives you all files in the current directory which begin by elfutils

dir | grep -i elfutils
elfutils-0.137-3.el5.i386.rpm
elfutils-devel-0.137-3.el5.i386.rpm
elfutils-devel-static-0.137-3.el5.i386.rpm
elfutils-libelf-0.137-3.el5.i386.rpm
elfutils-libelf-devel-0.137-3.el5.i386.rpm
elfutils-libelf-devel-static-0.137-3.el5.i386.rpm
elfutils-libs-0.137-3.el5.i386.rpm

Remove a file?

Permission: In UNIX and Linux, the ability to remove a file is determined by the access bits of parent directory

chmod 777 .
chown hi-adm:hi-adm .

With the command rm

Example:

rm filename.extension
rm -i filename.extension
rm -f filename.extension
rm -r directory

More … perform the man commando

man rm

Copy 1 or multiple file

cp - Copy one or more files to another location.

Example::

cp -r dirtocopy newdir
cp myFile1.txt myFile2.txt myDirDestination
cp * myDirDestination
# or
cp /myDirSource/* /myDirDestination
cp *.txt myDirDestination

Move / Rename

Example:

mv file.log file.$(date "+%Y.%m.%d-%H.%M.%S").log

Concatenate

cat file1 file2 > finalFile

# example with cert
cat first_cert.pem second_cert.pem > combined_cert.pem

Transfer

See the content

Search a file by name

Locate all file with a regular expression. In this example all file which begin with sp and end up with the extension msb.

locate -r "sp.*\.msb"
find /my/directory -name myFile.extension
#
find /my/directory -name globPattern

Search files based on time

touch -d '2011-12-31 10:22' foo
find . -newer foo

See all other files in the time section of the find command

Search the content of files

Shell Data Processing - Global Regular Expression Print (GREP command) (line filtering, word search)

grep -rn "string*" .

File Architecture (32 or 64 bit)

File Architecture

file fileName

Example:

file java
/bin/java: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped

Iterate / List

The ls pattern does not work in a bash script unless an option is on.

The best way is to use find when you need pattern matching

ENV_FILES=$(find . -maxdepth 1 -type f -name $ENV_FILE_PATTERN 2> /dev/null)
for ENV_FILE in $ENV_FILES ; do
    echo $ENV_FILE
done

Executable Capability

A file may also be an executable one and it may got capabilities such as being able to take the port 80

On Linux, see:

sudo setcap 'cap_net_bind_service=+ep' /path/to/your/executable
sudo getcap /path/to/your/executable

Processing

Conditional Expression

bash conditional expression on file 1)

-a file
True if file exists.

-b file
True if file exists and is a block special file.

-c file
True if file exists and is a character special file.

-d file
True if file exists and is a directory.

-e file
True if file exists.

-f file
True if file exists and is a regular file.

-g file
True if file exists and its set-group-id bit is set.

-h file
True if file exists and is a symbolic link.

-k file
True if file exists and its "sticky" bit is set.

-p file
True if file exists and is a named pipe (FIFO).

-r file
True if file exists and is readable.

-s file
True if file exists and has a size greater than zero.

-t fd
True if file descriptor fd is open and refers to a terminal.

-u file
True if file exists and its set-user-id bit is set.

-w file
True if file exists and is writable.

-x file
True if file exists and is executable.

-G file
True if file exists and is owned by the effective group id.

-L file
True if file exists and is a symbolic link.

-N file
True if file exists and has been modified since it was last read.

-O file
True if file exists and is owned by the effective user id.

-S file
True if file exists and is a socket.

file1 -ef file2
True if file1 and file2 refer to the same device and inode numbers.

file1 -nt file2
True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.

file1 -ot file2
True if file1 is older than file2, or if file2 exists and file1 does not.

List of command

Content