Linux file management
A full stop '.' before the name of the file makes it a hidden file. Example '.bash'
Using Parameters Expansion Removal
fileFullName=$(basename "$filePath")
# or
fileFullName="${filePath##*/}"
extension="${fileFullName##*.}"
filename="${fileFullName%.*}"
VAR=/filedoesnt/exist
echo ${VAR%/*}
/filedoesnt
dirname "dir/file"
dir
Linux - File/Folder Permissions - Access Control List ( ACL ) - Posix Model
if [ ! -f $PATH ]; then
echo "File $PATH not found!"
fi
ABSOLUTE_PATH=$(realpath "${FILE_NAME}")
with Linux - ls (List directory content)
ls -lh myFile | awk '{ print $5 }'
where:
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"
cmp --silent $old $new || echo "files are different"
cmp -b $old $new
See also : diff
diff file1 file2 | cat -t
where:
See also: comparison
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
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
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
Example:
mv file.log file.$(date "+%Y.%m.%d-%H.%M.%S").log
cat file1 file2 > finalFile
# example with cert
cat first_cert.pem second_cert.pem > combined_cert.pem
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
touch -d '2011-12-31 10:22' foo
find . -newer foo
See all other files in the time section of the find command
Shell Data Processing - Global Regular Expression Print (GREP command) (line filtering, word search)
grep -rn "string*" .
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
OLDIFS=$IFS; IFS=$'\n';
filelines=`cat $filename`
for line in $filelines ; do
echo $line
done
IFS=$OLDIFS
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