Linux file management command syntax, parameters, instance summary

Command: cat

The cat command is used to concatenate and display the contents of files. It can also be used to create new files or append content to existing ones.

Use Rights

All users

Syntax Format

Cat [-AbeEnstTuv] [--help] [--version] fileName

Parameter Description

-n or --number: Number all output lines starting from 1.

-b or --number-nonblank: Number non-blank lines only.

-s or --squeeze-blank: Replace multiple blank lines with a single one.

-v or --show-nonprinting: Display non-printing characters using ^ and M- notation, except for LFD and TAB.

-E or --show-ends: Show $ at the end of each line.

-T or --show-tabs: Display TAB characters as ^I.

-e: Equivalent to -vE.

-A, --show-all: Equivalent to -vET.

Instance

Add line numbers to textfile1 and save it in textfile2:

Cat -n textfile1 > textfile2

Append the contents of textfile1 and textfile2 to textfile3, without numbering blank lines:

Cat -b textfile1 textfile2 >> textfile3

Empty the contents of /etc/test.txt:

Cat /dev/null > /etc/test.txt

Cat can also be used to create disk images. For example, to create an image of a floppy disk:

Cat /dev/fd0 > OUTFILE

To write the image back to the floppy disk:

Cat IMG_FILE > /dev/fd0

Note: Ensure the device capacity matches when writing back from an image file. This is commonly used for creating boot disks.

Command: chattr

The Linux chattr command is used to change file attributes on ext2 file systems.

These attributes include: immutable, no access time update, compressed, not dumped, not changed, secure deletion, synchronous updates, and not deletable.

Syntax Format

Chattr [-RV][-v][+/-/=][file or directory...]

Parameter Description

-R: Recursively process directories.

-v: Set the file version.

-V: Display detailed information.

+: Enable the attribute.

-: Disable the attribute.

=: Set the attribute.

Instance

Prevent a critical file from being modified:

Chattr +i /etc/resolv.conf

Let a file only allow appending data:

Chattr +a /var/log/messages

Command: chgrp

The Linux chgrp command changes the group ownership of a file or directory.

Syntax Format

Chgrp [-cfhRv][--help][--version][group][file or directory...] or chgrp [-cfhRv][--help][--reference=][--version][file or directory...]

Parameter Description

-c or --changes: Show only changes made.

-f or --quiet: Suppress error messages.

-h or --no-dereference: Modify symbolic links only.

-R or --recursive: Process recursively.

-v or --verbose: Display detailed information.

--reference: Set the group based on another file.

Instance

Change the group of a file:

Chgrp -v bin log2012.log

Change the group based on a reference file:

Chgrp --reference=log2012.log log2013.log

Command: chmod

Linux/Unix file permissions are divided into owner, group, and others. Use chmod to control access to files.

Use Rights

All users

Syntax Format

Chmod [-cfvR] [--help] [--version] mode file...

Parameter Description

Mode: Permission setting string with format [ugoa...][[+-=][rwxX]...][,...].

Other parameters like -c, -f, -v, -R, etc., are available for detailed control.

Instance

Make a file readable by everyone:

Chmod ugo+r file1.txt

Set specific permissions using numeric values:

Chmod 777 file

This sets read, write, and execute permissions for all users.

Command: chown

The Linux chown command changes the owner of a file or directory.

Only root has permission to change ownership of other users' files.

Syntax Format

Chown [-cfhvR] [--help] [--version] user[:group] file...

Parameter Description

User: New owner of the file.

Group: New group of the file.

Instance

Change the owner of a file:

Chown jessie:users file1.txt

Change ownership recursively:

Chown -R lamport:users *

Command: cksum

The cksum command checks the CRC of a file to ensure its integrity during transfers.

It uses a checksum algorithm to detect errors.

Syntax Format

Cksum [--help][--version][file...]

Parameter Description

--help: Online help.

--version: Display version information.

Instance

Check the integrity of a file:

$ cksum testfile1

Output: 1263453430 78 testfile1

If the file is modified, the checksum will change.

Command: cmp

The cmp command compares two files for differences.

If the files are identical, no output is displayed. Otherwise, it shows the first differing character and position.

Syntax Format

Cmp [-clsv][-i ][--help][first file][second file]

Parameter Description

-c: Display characters along with their decimal codes.

-i: Ignore initial characters.

-l: Display all differences.

Instance

Compare two files:

Cmp prog.o.bak prog.o

If they differ, the output might be: prog.o.bak prog.o differ: char 4, line 1.

Command: diff

The diff command compares text files line by line.

It can also compare directories, but not subdirectories.

Syntax Format

Diff [-abBcdefHilnNpPqrstTuvwy][-][-C ][-D ][-I ][-S ][-W ][-x ][-X ][--help][--left-column][--suppress-common-line][file or directory 1][file or directory 2]

Parameter Description

-c: Display all text and mark differences.

-y: Side-by-side comparison.

Instance

Compare two files:

[root@localhost test3]# diff log2014.log log2013.log

Output includes differences in lines and added/deleted content.

Command: diffstat

The diffstat command displays statistics from diff outputs, showing insertions, deletions, and modifications per file.

Syntax Format

Diff [-wV][-n ][-p ]

Instance

Use diff and pipe to diffstat for statistical results:

$ diff test1 test2 | diffstat

This helps in quickly understanding the changes between files.

Command: file

The file command identifies the type of a file.

Syntax Format

File [-bcLvz][-f ][-m ...][file or directory...]

Parameter Description

-b: Do not display the file name.

-c: Display detailed execution steps.

-L: Display the category of the file pointed to by a symbolic link.

Instance

Identify a file's type:

[root@localhost ~]# file install.log

Install.log: UTF-8 Unicode text

[root@localhost ~]# file -i install.log

Install.log: text/plain; charset=utf-8

Command: find

The find command searches for files in specified directories.

Syntax Format

Find path -option [ -print ] [ -exec -ok command ] {} ;

Parameter Description

Options like -name, -type, -mtime, and -perm are used for filtering.

Instance

List all .c files in the current directory and subdirectories:

# find . -name "*.c"

Find all files modified within the last 20 days:

# find . -ctime -20

Delete files older than 7 days:

$ find /var/logs -type f -mtime +7 -ok rm { } ;

Command: git

The git command is a text-based file manager with an interactive interface.

It allows users to navigate, edit, and manage files easily.

Instructions

F1: View help information.

F2: List file contents.

F3: View file contents.

F4: Edit file contents.

F5: Copy files or directories.

F6: Move or rename files or directories.

F7: Create a directory.

F8: Delete files or directories.

F9: Execute make commands.

F10: Exit the git file manager.

Command: gitview

The gitview command displays file contents in both hexadecimal and ASCII formats.

Syntax Format

Gitview [-bchilv][file]

Parameter Description

-c: Display in color mode.

-h: Show help instructions.

Instance

View the contents of a file in color mode:

$ gitview -c /home/rootlocal/demo.txt

Command: indent

The indent command adjusts the formatting of C source code files.

It helps in making code more readable by applying consistent indentation and spacing rules.

Syntax Format

Indent [parameter][source file] or indent [parameter][source file][-o target file]

Parameter Description

Various options control indentation, braces placement, comment formatting, etc.

Instance

Format a C source file with default settings:

$ indent -linux program.c

Adjust indentation level to 4 spaces:

$ indent -i4 program.c

Agriculture Sprayer Drone

1.Farm Agriculture Spray Drone Uav With Gps,
2.We have5L/10L/20L/30L/50L,4-rotor,6-roto,8-roto drone
3.Hourly working area: 6 hectares,Autonomous flight
4.Can be used for spraying, sowing, and fertilizing

Agriculture Sprayer UAV,Agriculture sowing Drone,Agricultural fertilization drone

Jiangsu Yunbo Intelligent Technology Co., Ltd , https://www.fmodel-ai.com

This entry was posted in on