File management basic commands

0x00 Quick Start

Command prompt format:

//Example:
[root@localhost ~]#

//illustrate:
root:   currently logged in user   root Yes Linux medium admin user
localhost: CPU name
 ~        The current directory (home directory)
 #       superuser prompt
 $       The prompt for a normal user is
copy

Command and file completion: Command and file completion in Bash (sh) are very convenient and commonly used functions. We only need to press the Tab key to complete the completion automatically when entering a command or file. Linux command writing specification: a complete linux basically consists of two parts, commands and parameters, separated by spaces, and connected with "-" or "-"

Types/permissions of files and directories:

#Types of files and directories
drwxr-xr-x : d Representative directory 
-rwxr-xr-x : - Representative file 
lrwxrwxrwx : l soft link file
crw-rw---- : c character device
brw-rw---- : b block device
srw-rw-rw-: 

#permission
drwxr-xr-x. #Permission 10
#r means read, w means write, x means execute, - means no permission
rw-  r—x   r—x   #no trio
rw- on behalf of the owner    r--represent the group    r--represent others
copy

dir command

Usage and parameters:

$dir [Options][file or directory] #File viewing is similar to ls
-a Show all files, including hidden files
-l show details
-d View directory properties
-h Humanized display file size
-i show inode
copy

Example:

#Example 1: Display all files and humanize display size and inode
[root@izwz9biz2m4sd3bb3k38pgz ~]$dir -lhi
total 5.6M
139002 -rwxr-xr-x 1 root root  994 May 13 22:40 dingding.py
139000 -rw-r--r-- 1 root root 5.6M May  6  2018 dingding.sh
copy

ls Command - View Files and Directories

Description: Display directory and file list dirctory

ls [Options][file or directory]
#[options]
-a, --all Also displayed with.content at the beginning,include.Table of contents(Current directory)and..Table of contents(parent directory).
-A, --almost-all Also displayed with.content at the beginning,does not include.directory and..Table of contents.
     --author In use-l The file creator is also displayed when the parameter is used.
-B, --ignore-backups does not display with~end of file(backup file).
-c, (default)Display items sorted by last modification time,new in front.-lt Sort entries by last modification time,and show the modification time.-l show modification time,but in alphabetical order.
-C ,list entry for column.
      --color[=WHEN] Set whether to use color to distinguish item categories,WHEN can be never,always and auto.never and always it's clear,auto means standard output and terminal Only show color when binding.Color by environment variable LS_COLORS Decide.
-d, --directory list directory entries,and does not list shortcuts.
-f List not sorted,enable-aU,Also disable-ls and--color.
-F file type( File type)
-g similar-l,but doesn't show except owner.
-G, --no-group exist-l group names are not listed in.
-h, --human-readable exist-l List file sizes in a more understandable way. (e.g., 1K 20M)
     --si Ditto,but the multiple is 1000 instead of 1024
-i, --inode show each entry index number.
-I(i uppercase), --ignore=PATTERN does not list shell style entry.(Excluded files are not displayed)
    --hide=PATTERN but will be-a or-A cover.
-l Display information in a detailed list mode.
-m All items are displayed separated by commas.
-n, --numeric-uid-gid similar-l,List both user and group information.
-o similar-l,but does not list group information.
-p, --indicator-style=slash add after the directory/
-r, --reverse Display in reverse order.
-R, --recursive list all subdirectories.
-s, --size Shows the space allocated for each file.
-S Sort files by size.
    --sort=WORD according to WORD to sort,can be none,extension,size,time and version.
    --time=WORD exist-l inside,according to WORD instead of modification time,can be atime,access,use,ctime,status.
-t Sort by modification time,new in front.
-U not sorted,According to the original order of the system.
-v Sort by version number.
-x in row order.
-X Sort by extension.
-1 Display only one item on a line.
--full-time : Enter in full time mode
--time={atime,ctime} : Output access time or change permission attribute time(ctime)not when the content changed(mtime)
--color={never,always,auto} : no way/display color/L 

#return value
0 normal.
1 small issue(e.g., cannot enter subdirectories).
2 Serious Problem(e.g., Cannot enter the directory displayed by the command line arguments).
copy

Example:

#0. Do not show . and .. directories
$ls -A

#1. Exclude files and directories other than dingding.sh from being displayed
$ls -I dingding.sh
dingding.py  quartz_uninstall.sh  startWechat.sh  uninstall.sh
$ls -I *.yml  #Without single and double quotes, it will match and display the same configuration
handlers.yml  listen.yml  notify.yml  tag.yml  test.yml  version.yml
$ls -I "*.yml"  #i.e. the reverse match shows
10.10.107.222.txt  listen.yml.save  notify.yml.save  test.txt
$ls -I "*5" -I "*0" |xargs rm # Pass the file to be deleted as an argument to the rm command through xargs


#2. Humanized display of all files and file inode s
$ls -ialh
131087 -rw-------   1 root root  16K May 31 12:32 .bash_history

#3. List the PID of the file
$ls -n
total 5692
-rwxr-xr-x 1 0 0     994 May 13 22:40 dingding.py

#4. Display files by extension
$ls -Xl
drwxr-xr-x 2 root root    4096 Mar  1 10:35 pythonDemo
drwxr-xr-x 2 root root    4096 May 31 14:44 www
-rwxr-xr-x 1 root root     994 May 13 22:40 dingding.py
-rw-r--r-- 1 root root 5803476 May  6  2018 dingding.sh

#5. Sort
ls -1h --sort=size   #Sort by size
ls -lhs  
ll -Sh
ls -1h --sort=time   #Sort by time
ls -lht
ls -laht 
ls -lht -r           #Sort in reverse chronological order

#6. Do not display owner/group and convert permissions to numbers (uid/guid number)
$ls -lG
$ls -ln

#7. Display the security context of files and directories (Selinux)
ls -alhZd /root/ #ll -Zd .
dr-xr-x---. root root system_u:object_r:admin_home_t:s0 /root/

#8. Display the modification time of the complete file
$ls -alh --full-time /etc/group
-rw-r--r-- 1 root root 735 2020-05-09 17:10:25.795053900 +0800 /etc/group

# 9. View modification time/access time/status modification time
❯ ls -al core-js-banners; ls -al --time=atime core-js-banners; ls -al --time=ctime core-js-banners
-rw-r--r-- 1 root root 1 Oct 18 12:55 core-js-banners  # Change the time
-rw-r--r-- 1 root root 1 Oct 10 09:48 core-js-banners  # interview time
-rw-r--r-- 1 root root 1 Oct 18 12:55 core-js-banners  # Permission attribute time

# 10. Count the number of files under the folder including sub-files and count the number of directories under the folder, including sub-directories
ls -lR | grep "^-"| wc -l
ls -lR | grep "^d"| wc -l

# 10. View the executable files in the directory.
# Symbols include: / indicates a directory; @ indicates a symbolic link to another file; * indicates an executable file
ls -F | grep "/$"
ls -F | grep "@$"
ls -F | grep "*$"
  # mkreleasehdr.sh*
  # redis-benchmark*
  # redis-check-aof*
  # redis-check-rdb*
  # redis-cli*
  # redis-sentinel*
  # redis-server*
  # redis-trib.rb*
copy

WeiyiGeek.ls

Additional instructions:

#As an example to illustrate:
-rw-r--r--[10 characters] 1[hardwired] root root 5803476 May  6  2018 dingding.sh

# The first column is the file type and permissions: the first character indicates the type of the file
 which is,-Indicates that it is a normal file, d represented as a directory, c represented as a character device, b Represented as a block device, to represent connected files
 The following nine digits: every 3 characters represents the owner, group and others permission
 in r express read,w express write,x express execute,-Indicates no permission


#2. The second column represents the number of hard links:
Each time a hard link is added to the file, the number will increase by 1, starting from 1 by default
1 Indicates that there are no hard-linked files,
2 Indicates that he is a directory,Note that directories cannot be hard links

#3. The third column is the owner (user) of the file (directory)
#4. The fourth column is the group of the file (directory)
#5. The fifth column is the size of the file (directory), in bytes (size)
#6. The sixth column is the file (directory) creation time or last access time
 The order is month, day, time,If the time is too long from now, the year will be displayed directly

Note: for directories if there is no w Permission, even if the file inside has write permission, the file cannot be moved or renamed.,At this point, you need to add the directory w permission
copy

cd command - directory switching

Description: change directory command (change Directory)

Common commands:

# conventional
cd /Table of contents

# Five special categories
cd ~  or cd /root/ back home directory
cd ~weiyigeek directly to weiyigeek home directory
cd -  go back to the last directory
cd .. go back to the previous directory
cd .  enter the current directory
copy

Basic example:

❯ useradd -m weiyi
❯ cd ~weiyi
/home/weiyi
copy

Tips: Shortcut description ctrl+L to clear the screen, press the tab key twice to complete the directory/command;

pwd command - show current directory

Description: Get the current directory location i.e. Print Working Directory;

Basic syntax and important parameters:

pwd [-P]

# parameter
-P : Show current path instead of using connection(Link)path
copy

Practical example:

# 1. Get the current directory location
[root@localhost home]$pwd
/home

# 2. The similarities and differences of the -P parameter show the actual working directory instead of the directory where the link exists
❯ ls -ld /var/lock
lrwxrwxrwx 1 root root 9 Apr 23 14:40 /var/lock -> /run/lock
❯ cd  /var/lock
❯ pwd
/var/loc    # Contrast point

❯ pwd -P   
/run/lock   # Contrast point
copy

Tips: Two path representations;

absolute path: /root/directory
 relative path: ~/directory
copy

mkdir command - create a new directory

Description: Create Directory Command (Make Directory)

Basic syntax and important parameters:

mkdir -vp directory name 

# parameter
-p to create recursively
-m Configure permissions when creating directories without looking at default permissions(umask)
-v Show build process
copy

Simple example:

#Example 1. Build multiple directories recursively
mkdir -vp /tmp/{oa,bbs,video}  #Create a directory using an array
mkdir: created directory '/tmp/oa'
mkdir: created directory '/tmp/bbs'
mkdir: created directory '/tmp/video'


#Example 2. Create parent directories and then create subdirectories within each parent directory
mkdir -vp /tmp/{a,b,c}/{test1,test2}
# mkdir: created directory '/tmp/a'
# mkdir: created directory '/tmp/a/test1'
# mkdir: created directory '/tmp/a/test2'
# mkdir: created directory '/tmp/b'
# mkdir: created directory '/tmp/b/test1'
# mkdir: created directory '/tmp/b/test2'
# mkdir: created directory '/tmp/c'
# mkdir: created directory '/tmp/c/test1'
# mkdir: created directory '/tmp/c/test2'

#Example 3. Create directory division permission assignment
❯ mkdir -m 711 weiyigeek  # Create a directory with rwx--x--x permissions
❯ ls -ld /tmp/weiyigeek
drwx--x--x 1 root root 4096 Oct 18 12:31 /tmp/weiyigeek
copy

rm command

Description: Remove Empty Directory, remove non-empty directories or files; TIPS: Use rm -rf / with caution, all files in the root directory will be deleted;

#Basic grammar
rmdir[Directory name]
rm [Options][Table of contents/document]

#rm parameter
-i Interactive prompt before deletion
-r Delete folders recursively, including empty folders
-f Force unconditional deletion of read-only files
copy

Example:

#Example 1.
rm -rf / : Force unconditional deletion root everything in the directory.
rm -rf * : Force unconditional deletion of all content in the current directory.
rm -rf . : Force unconditional deletion of current folder and subfolders

#Example 2. Delete files or directories other than excluded files
rm `ls -I demo.config`

#Example 3. Add a backslash before the rm command to ignore the specified parameter of alias and delete this directory without asking;
\rm -r /tmp/demo.txt
copy

rmdir command - delete an empty directory

Description: Delete an empty directory and no other directories or files can exist in that directory.

Syntax parameters:

-p : delete together with the parent directory
copy
#Example 0: Recursively delete all empty directories
rmdir -p *
copy

cp command - file directory copy

Description: Copying files and directories is a basic instruction of every operating system. The act of backup is basically creating copies of files and directories. In Linux, we can use the cp command to achieve this, but we need to pay attention to the following points when copying:

  • 1. Whether the information of the source file needs to be kept intact.
  • 2. Whether the source file is a soft link file (symbolic link file)
  • 3. Whether the source file is a special file such as FIFO/socket, etc.
  • 4. Whether the source file is a directory

Syntax parameters:

cp [Option] [Original file/Directory] [Target Directory]
cp [-adfilprsu] source1 source2 destination

#parameter:
-a equivalent to-pdr The options are guaranteed to be exactly the same as the original file attributes(The attributes of the original file or directory are preserved),but inode number is changing
-r recursive continuous replication(Table of contents/subdirectory/documents, etc.)
-p Copies with file attributes
-u differential replication(Only new or changed files will be copied, and files with the same attributes and existing will not be copied again),Commonly used in backup
-f If a file with the same file name already exists at the destination, delete it before copying and then copy it
-i If the target file already exists, the operation will be asked first when overwriting(Commonly used). 
-l Create shortcuts to link to files(Note that hard links cannot be used to create directories,and have the same inode No)
-d If the original file is a linked file, copy the link attributes (excluding attributes)
-s Copy as link symbol file(symbolic link) i.e. shortcut file( soft link)
-u like dest Compare source old update destination
-P Copy only symlink files(Do not copy original files with symbolic links)
-L Copy the original file with the symlink(with the above -P option instead)
-v Show file copy process
--backup Make a backup of each existing target file,The optional parameters are as follows.
  # none, off: never backup (even if --backup is given)
  # numbered, t: backup with number
  # existing, nil : use the numbered backup if the numbered backup exists, otherwise use the simple backup
  # simple, never : always use simple backup
--attributes-only    Only the file name and its attributes will be copied, no data will be copied
--remove-destination if cp The command finds a file with the same name in the target directory, cp The command deletes the target file first, and then makes a new copy.
copy

Basic example:

#Example 1. Basic example usage, moving a file or directory from one location to another
cp -a weiyi /Users/WeiyiGeek/Documents       # Copy the weiyi directory to the manuscript directory (with the same attributes as the above effect)
cp -rpd weiyi/ /Users/WeiyiGeek/Documents    # Copy the directory in the weiyi directory to the manuscript directory
cp -r directory_1 /home/pungki/office        # The -r or -R option indicates recursive operation (one thing to note is that you need to remove the trailing slash from the directory name, otherwise you will get an error message like cp: omitting directory 'directory_1/')

#Example 2. The copied id number is changed but the attributes are the same
$cp -a 1.txt 2.txt && ls -i
393342 -rw-r--r-- 1 root root 0 May 31 15:31 1.txt
393343 -rw-r--r-- 1 root root 0 May 31 15:31 2.txt

#Example 3. Simplify cp to directly copy files with the specified suffix
$cp 1.txt{,.bak} && ls
1.txt  1.txt.bak

#Example 4. Force override without prompting for override
$cp -fn 1.txt demo/1.txt 

#Example 5. In interactive mode, you will be asked whether to overwrite the files in the target directory. Use the -i option to enable interactive mode.
cp -ir directory_1/ /home/pungki/office/
$yes | cp /etc/fstab /etc/fstab_bak    # cp: Overwrite "/etc/fstab_bak"? 
$yes | cp -i /etc/fstab /etc/fstab_bak # cp: Overwrite "/etc/fstab_bak"? 

#Example 6. If ~/.bashrc is newer than /tmp/bashrc, copy it over to overwrite /tmp/bashrc
cp -u ~/.bashrc /tmp/bashrc

#Example 7. Difference of -d parameter vs bashrc_link -> bashrc and creating hard links to files instead of copying them
cp bashrc_link bashrc_link_1            # Copy the actual file of the linked file (copy according to the original file attributes)
cp -d bashrc_link bashrc_link_2         # copy link file 
cp -l file_4.txt /home/pungki/office    # Create "shortcuts" or links to files instead of copying them (have the same inode value as the original directory files)
cp -s /home/pungki/office file_4.txt    # Creating a symbolic link can only be done in the current directory (no inode value)
cp -P file_6.txt ./movie                # Use the cp command on a symlink and it will copy itself as-is
cp -L file_6.txt ./movie                # Copy the original file with the symlink
cp -v *.txt /home/pungki/office         # Show copy job process


#Example 8. Copy the main folder .bashrc and bash_history to /tmp (copying multiple files at the same time requires separating multiple files with spaces)
cp ~/.bashrc ~/.bash_history /tmp

#Example 9. Copy when the original file is newer than the target file
$ cp -vu *.txt /home/pungki/office

#Example 10. When the target directory already contains a file with the same name, by default the cp command will overwrite the file with the same name in the target directory. Using the --backup option, the cp command will make a backup for each existing target file. ../office is relative to /home/pungki/office .
$ cp --backup=simple -v *.txt ../office # The --backup=simple option creates a backup file with a tilde (~) at the end of the filename

#Example 11. Copy only file attributes without copying file data
$ cp --attributes-only file_6.txt -v ../office

#Example 12. Using the -f option forces a copy operation. If the target file cannot be opened, try it with -f.
$ cp -f *.txt -v ../office

#Example 13. Delete the target before copying (if the cp command finds a file with the same name in the target directory, the cp command will delete the target file and then copy a new one)
cp --remove-destination *.txt -v ../office
copy

Tips: When we normally use cp Order on Linux, we find it annoying to copy documents from one directory to another with the same document name, even if -rf parameter is added to force copying to be overwritten, you will still be prompted to enter y manually to confirm the copy one by one. So what's wrong?

Cause: The alias of the cp command exists in the alias command.

# Note: Although we usually use the cp command without adding any parameters, the system will automatically add the -i parameter when we use the cp command by default (that is, the abbreviation of interaction)
alias cp='cp -i'
copy

Solution: Remove the alias of the cp command. When using cp -rf to copy files, it will not ask for confirmation.

unalias cp
copy

mv command

Description: Cut or rename command (move)

mv [Original file or directory] [target directory]  #Rename in the same directory
mv specified folder/document /dev/null

# parameter
-f: Forced move without warning message
-i: Interactive mode, asks you before moving
-u: When the target exists, only source Update only if newer
-t: --target-directory=DIRECTORY  #Move all source parameters into a directory
copy

Basic example:

#Example 1: /dev/null or null is a special file under linux, they will discard all the data written to themselves, and then return a message that the operation is successful.
mv 1.txt /dev/null #This mv command will move the specified folder to /dev/null.
mv /home/user/* /dev/null #The above command will move all the content under /home/user/ to /dev/null.

#Example 2: Simple haul syntax (commonly used for backup)
$mv pam_unix.so{,.bak}   #directly into pam_unix.so.bak

#Example 3. Move the source directory to the specified directory in batches
tmp$ ls www/
# 10.txt  1.txt  {1..txt}  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt
tmp$ mv -t backup/ www/*
tmp$ ls backup/
10.txt  1.txt  {1..txt}  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

#Example 4. If there are multiple source files or directories, the last target file must be a directory
mv bashrc1 bashrc2 mvtest2
copy

touch command - modify actual file or create new file

Description: The touch command is not commonly used. Generally, when using make, it may be used to modify the file timestamp or create a new file that does not exist;

Its three main change times:

1.modification time : that is, mtime will be updated when the file content data changes; note that this refers to the content of the file rather than the attribute permissions of the file;

2.status time: that is, ctime will be updated when the status of the file changes; for example, permissions and attributes are changed;

3.access time: that is, atime will be updated when the content of the file is read; for example, using cat to read /etc/man.config will update it;

❯ ls -lh /etc/manpath.config
-rw-r--r-- 1 root root 5.1K Feb 26  2020 /etc/manpath.config   # mtime 

❯ ls -lh --time=atime /etc/manpath.config
-rw-r--r-- 1 root root 5.1K Apr 23 14:41 /etc/manpath.config   # atime 
                               
❯ ls -lh --time=ctime /etc/manpath.config
-rw-r--r-- 1 root root 5.1K May  9 16:29 /etc/manpath.config   # ctime
copy

Syntax parameters:

usage: touch [Options] document 

# parameter
-a or--time=atime or--time=access or--time=use  Only change the access time.
-c or--no-create  No documentation is created.
-d Use the specified datetime instead of the current time.
-f This parameter will be ignored and not processed, only responsible for solving BSD Version touch Command compatibility issues.
-m or--time=mtime or--time=modify  Only change the change time.
-r Set the date and time of the specified document or directory to be the same as the date and time of the referenced document or directory.
-t Use the specified datetime instead of the current time use [[CC]YY]MMDDhhmm[.ss]
copy

actual case:

# Example 1. Create a file and modify the file at a specified time
$touch create.txt
$touch -t 201908031122 1.txt
$ls -lh
-rw-r--r-- 1 root root 0 Aug  3  2019 1.txt
-rw-r--r-- 1 root root 0 May 31 15:48 create.txt

# Example 2. Batch creation of files using {} symbols
touch hyml-{1..10}.html

# Example 3. Time adjustment Modify the authentication of a file to the current date (mtime and atime)
❯ echo > hello.txt
❯ touch -d '2 day ago' hello.txt
❯ ls -alh hello.txt
-rw-r--r-- 1 root root 1 Oct 16 17:37 hello.txt
❯ ls -ailh hello.txt; ls -alhi --time=atime hello.txt;ls -ailh --time=ctime hello.txt
13229323905543610 -rw-r--r-- 1 root root 1 Oct 16 17:37 hello.txt  # Revise
13229323905543610 -rw-r--r-- 1 root root 1 Oct 16 17:37 hello.txt  # access
13229323905543610 -rw-r--r-- 1 root root 1 Oct 18 17:37 hello.txt  # Permission attribute change
copy

file command - view file types

Description: Use this command to know whether the basic data of a file belongs to ASCII or data file or Binary and uses dynamic function link library and other information;

Basic example:

# ASCII type
> ls -alh /tmp/weiyigeek.txt
-rwx--x--t 1 weiyigeek weiyigeek 9 10 Jan 20 05:52 weiyigeek.txt
> file /tmp/weiyigeek.txt
weiyigeek.txt: sticky , ASCII text

# binary program type
> file /usr/bin/passwd
/usr/bin/passwd: setuid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=fa23c6b4e145d9bb5487c6cb7adb9338c5ef4fb9, for GNU/Linux 3.2.0, stripped
copy

ln command

Description: Link creation command (Link), note that it is recommended to use an absolute path for the path of the source and target files.

ln -s [Source file path] [target path/file]  # The function is to generate a soft link file | must use an absolute path
-s Create a soft link (without this parameter)

#Example 1. Soft connection (absolute path must be used) ie /tmp/id_rsa.pub links to /root/.ssh/id_rsa.pub
ln -s /root/.ssh/id_rsa.pub /tmp/id_rsa.pub

#Example 2. Hardwired
$ln /root/.ssh/id_rsa.pub /tmp/id_rsa.pub
$ln create.txt create.txt.bak
393422 -rw-r--r--   2 root root    0 May 31 15:48 create.txt
393422 -rw-r--r--   2 root root    0 May 31 15:48 create.txt.bak
copy

WeiyiGeek.ln command example

Supplementary knowledge: Features of soft links:

  • Similar to the shortcut under win, modifying any file will change the other (the file of the same inode is modified)
  • Soft links have their own i-nodes and block blocks, but only the file name and i-node number of the original file are stored in the data block, and there is no actual file data.
  • lrwxrwxrwx l Soft link Note that the permissions of the soft link file are all rwxrwxrwx
  • Soft links cannot be used after deleting the original file
  • absolute path must be used

Characteristics of hard links:

  1. Hard links with the same i-node and storage block can be regarded as the same file
  2. Recognizable by inode ls -i (same inode)
  3. Cannot span partitions and cannot be used against directories
  4. Delete one Ningwai one can also open

rename command

Description: This command can batch change the names of all files in the directory through wildcards;

Syntax parameters:

Usage: rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E *perlexpr*]*|*perlexpr*
    [ *files* ]

#parameter:
-v Indicates that the successfully modified file name will be displayed;
-n It means that no operation is performed and it is mainly used for testing rename The process does not run directly, you can check the test effect and then run it
-f It means that the modification will be forced.
copy

Basic example:

#File example 1:
for i in {1..20};do
 touch "demo_"${i}".sh"
done


#Change the file prefix demo -> weiyigeek
rename  -v  's/_/ /g' *
demo_10.sh renamed as demo 10.sh
#Centos7 can
rename -n "demo" "weiyigeek" *.sh #Mainly matches sh files


#test changes
rename -n 's/demo /weiyigeek-/g' *.sh  #Note the wildcard here
# rename(demo 10.sh, weiyigeek-10.sh)
# rename(demo 11.sh, weiyigeek-11.sh)
# rename(demo 12.sh, weiyigeek-12.sh)
# rename(demo 13.sh, weiyigeek-13.sh)

#Convert all uppercase of the file name to lowercase as
rename -n 'y/A-Z/a-z/' *
# rename(demo 10.sh, DEMO 10.SH)
# rename(demo10.sh, DEMO10.SH)
# rename(demo 11.sh, DEMO 11.SH)
copy

tempfile command - Debian

Description: This command is based on the default in Debian distributions such as Ubuntu other distributions do not have this command; The naming method and random number of temporary files, sometimes when writing Shell scripts, some temporary data storage is required. The most suitable location for storing temporary file data is /tmp, because all the contents in this directory will be deleted after the system is restarted. will be cleared.

tempfile Order
$RANDOM is an environment variable that returns a random number

#Method 1. Use the tempfile command to name a temporary file:
temp_file_name=$(tempfile)

#Method 2. Use a file name with a random number as a temporary file name:
temp_file_name="/tmp/file_$RANDOM"
copy

WeiyiGeek.tempfile

If you don't have a Linux distribution with the tempfile command, you can also use your own temporary filename:

#$$ is a system predefined variable, which displays the process id of the current process. The suffix added with .$$ will be expanded to the process id of the currently running script.
temp_file_name="/tmp/file.$$"

#Example 1:
$echo $temp_file_name
/tmp/file.7526 #current pid process number

#Example 2:
$touch file_$$ && ls
create.txt  create.txt.bak  file_7526
copy

Tags: Linux

Posted by 3dron on Wed, 28 Sep 2022 23:35:58 +0930