Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement File manipulation by Linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces Linux how to achieve file operation, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

File is an important concept in linux. In Linux, everything (almost everything) is a file. To put it simply, the basic printf () function and scanf () function in C are all file operations.

Other ways to create new files

Nano is similar to the function of notepad under Windows. Nano filename can create a new file and write in it; ctrl+x exits and press Y to save when prompted. Vim is a more powerful text editor. Vim filename can create a new file, hit the letter I on the keyboard, and enter writing mode. When you are finished, click Esc, exit writing mode, type: W (which will be displayed in the lower left corner of the screen), and press enter to save. The common method of vim will be written alone in the future, so far as it is here.

File operation under Linux

Common file operations include moving files to another folder, copying files to another folder, renaming files, and so on. Cp (copy): copy a file or folder (cp-r parameters when copying a folder, recursive copy)

# list the files and folders in the current directory ct@ehbio:~$ lsdata# create a new folder ct@ehbio:~$ mkdir ehbio_project# list the files and folders in the current directory and the contents of their subfolders # there is a file in the data directory No files under the ehbio_project directory * data:test.faehbio_project:# copy the files under the data directory test.fa to the ehbio_project directory ct@ehbio:~$ cp data/test.fa ehbio_project/# lists the files and folders under the current directory, and the contents of their subfolders # there is a file under the data directory No files in the ehbio_project directory ct@ehbio:~$ ls * data:test.faehbio_project:test.famv (move): move files or folders

# rename the file test.fa in the data directory to first.fa # mv. In addition to moving files, you can also rename ct@ehbio:~$ mv data/test.fa data/first.fa for individual files.

# list the files and folders in the current directory, and the contents of their subfolders ct@ehbio:~$ ls * data: first.fa

Ehbio_project: test.fa

Rename: file renaming (often used for batch renaming, different systems may have slightly different usage, man rename to see how to use before using) # enter another directory ct@ehbio:~$ cd ehbio_project/ct@ehbio:~/ehbio_project$ lstest.fa# to make a copy of the file ct@ehbio:~/ehbio_project$ cp test.fa second.fact@ehbio:~/ehbio_project$ lssecond.fa test.fa# to copy the document several times Boring operation, in order to give rename a chance to play a role after the ct@ehbio:~/ehbio_project$ cp test.fa test2.fact@ehbio:~/ehbio_project$ cp test.fa test3.fact@ehbio:~/ehbio_project$ cp test.fa test4.fa# cp requires 2 parameters, the file to be copied and to be copied to the directory or file # the following error occurred Indicates missing destination path or file ct@ehbio:~/ehbio_project$ cp ehbio.facp: missing target file Try'cp after "ehbio.fa"-help' for more information.ct@ehbio:~/ehbio_project$ lssecond.fa test2.fa test3.fa test4.fa test.fa# batch renames ct@ehbio:~/ehbio_project$ rename 'test'' ehbio' test*.fact@ehbio:~/ehbio with rename feed files _ project$ lsehbio2.fa ehbio3.fa ehbio4.fa ehbio.fa second.faln (link): create a shortcut to the file (ln-s source_file target creates a soft connection).

When establishing a soft connection, the original file should use the full path. A full path refers to a path that begins with /.

The establishment of a soft connection is a way to simplify file access without increasing hard disk storage. Link the files in other folders to the current directory, and you only need to write the name of the file when you use it. There is no need to write a long list of directories.

.. /: indicates the upper level directory;.. /.. /: indicates the upper two-tier directory

Pwd (print current/working directory): the directory where the output is currently located

\ for the first key under the keyboard Esc (the same key as the home directory ~ `symbol), the command written in the backquotation mark will be run, and the running result will be placed in the location of the backquote.

# establish a soft connection and link the ehbio2.fa in the current directory to the data in the previous directory. # this is an invalid soft connection. When ct@ehbio:~/ehbio_project$ ln-s ehbio2.fa.. / data# is viewed using ls, the file name of the invalid soft connection has a black background under it. Total amount of ct@ehbio:~/ehbio_project$ ls-l.. / data/ 4lrwxrwxrwx 1 ct ct 9 June 9 17:55 ehbio2.fa- > ehbio2.fa-rw-rw-r-- 1 ct ct 284 June 8 14:48 first.fa# output the current directory ct@ehbio:~/ehbio_project$ pwd/home/ct/ehbio_project# to establish a soft connection, the original files must use the full path. A full path refers to a path that begins with /. Ct@ehbio:~/ehbio_project$ ln-s / home/ct/ehbio_project/ehbio2.fa.. / dataln: unable to create symbolic link ".. / data/ehbio2.fa": when the # error message already exists in the file, such a link already exists (although it is invalid) However, when you create a new link, you will also be prompted to use `- f` (force) to force overwriting the existing link ct@ehbio:~/ehbio_project$ ln-fs `pwd` / ehbio2.fa.. / data#, and it will be normal. There is no background color under the file name, and a right arrow pointing to `l` in the original file # lrwxrwxrwx` indicates a soft connection. Total amount of ct@ehbio:~/ehbio_project$ ls-l.. / data/ 4lrwxrwxrwx 1 ct ct 32 June 9 17:56 ehbio2.fa- > / home/ct/ehbio_project/ehbio2.fa-rw-rw-r-- 1 ct ct 284 June 8 14:48 first.fa# usually to simplify writing, use `pwd` instead of full path # `as the key under the keyboard Esc, and commands written in backquotes will be run The running result will be placed in the location of ct@ehbio:~/ehbio_project$ ln-s `pwd` / ehbio2.fa.. / dataln: cannot create symbolic link ".. / data/ehbio2.fa": ct@ehbio:~/ehbio_project$ ln-fs `pwd` / ehbio2.fa.. / datact@ehbio:~/ehbio_project$ ls-l. / data/ total usage 4lrwxrwxrwx 1 ct ct 32 June 9 17:56 ehbio2. Fa- > / home/ct/ehbio_project/ehbio2.fa-rw-rw-r-- 1 ct ct 284 June 8 14:48 first.fa thank you for reading this article carefully I hope the article "how to manipulate Linux documents" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report