Linux - File
> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU) > Bash - File System
Table of Contents
1 - About
Linux file management
2 - Articles Related
3 - Basic
4 - Type
4.1 - hidden
A full stop '.' before the name of the file makes it a hidden file. Example '.bash'
4.2 - Metadata
5 - Management
5.1 - Get
5.1.1 - Filename and extension
Using Parameters Expansion Removal
fileFullName=$(basename "$filePath") # or fileFullName="${filePath##*/}" extension="${fileFullName##*.}" filename="${fileFullName%.*}"
5.1.2 - parent directory
- From a path string where the file does not exist
VAR=/filedoesnt/exist echo ${VAR%/*}
/filedoesnt
dirname
returns the first parent of an existing path file.
dirname "dir/file"
dir
5.1.3 - File Permission
5.1.4 - File exist
if [ ! -f $PATH ]; then echo "File $PATH not found!" fi
5.1.5 - Absolute path
ABSOLUTE_PATH=$(realpath "${FILE_NAME}")
5.2 - Comparison
cmp --silent $old $new || echo "files are different" cmp -b $old $new
See also : diff
5.3 - Diff
5.4 - Filter a list of files ?
- Using grep
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
- find can do it recursviely
5.5 - 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:
- Basic
rm filename.extension
- Prompts for confirmation before removing a file
rm -i filename.extension
- Force removal of the file regardless of it bieng write-protected or open
rm -f filename.extension
- Remove files recursively in 'directory'
rm -r directory
More … perform the man commando
man rm
5.6 - Copy 1 or multiple file
cp - Copy one or more files to another location.
Example::
- Copy of a directory with resistivity:
cp -r dirtocopy newdir
- Copy two files
cp myFile1.txt myFile2.txt myDirDestination
- Copy all files of directory
cp * myDirDestination # or cp /myDirSource/* /myDirDestination
- Copy all txt files (with a pattern)
cp *.txt myDirDestination
5.7 - Move / Rename
- mv - Moves or renames file
5.8 - Transfer
5.9 - See the content
- cat,
- head (Displays first 10 lines of file),
- tail - Displays last 10 lines of file,
- tac (for reverse output)
- less and more let you scroll the text files
5.10 - Search a file by name
- locate - List files that match a pattern.
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 - search for files in a directory hierarchy.
find /my/directory -name myFile.extension
5.11 - Search files based on date
touch -d '2011-12-31 10:22' foo find . -newer foo
5.12 - Search the content of files
Linux - Global Regular Expression Print (GREP command) (line filtering, word search)
- For the current directory
.
grep -rn "string*" .
5.13 - 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
5.14 - Iterate
OLDIFS=$IFS; IFS=$'\n'; filelines=`cat $filename` for line in $filelines ; do echo $line done IFS=$OLDIFS
6 - List of command
7 - Content
- Sh - Uniq ("unique") - duplicate line removing: returns a file where the line are unique