📜 ⬆️ ⬇️

Unix as IDE: Files

One of the important features of the IDE is the built-in file management system. It should include both basic features, such as renaming, deleting a move, and more development-specific ones: compilation and syntax checking. In addition, it would be convenient to operate with groups of files to search by size, extension, or by mask. In this first article, I will show several options for using tools familiar to any Linux user to work with groups of files in a project.


File lists


The administrator is familiar with the ls command one of the first to use it to view a simple list of directory contents. Many administrators also know how to use the -a and -l keys to list all files, including the '.' and '..' , and to display more detailed information about the files by columns, respectively.

There are a few more keys for ls , some less commonly used, but very useful for programming purposes:

')
Since the file list is plain text, you can send it to vim , where you can add explanations to each file, and then save the text as an inventory or add it to the README:

 $ ls -XR | vim - 


Similar things can even be automated with make with little effort, which I will discuss in another article in this series.

File search


Oddly enough, you can get a complete list of files, including relative paths, by simply typing " find " with no arguments, although it is usually better to arrange them in order using sort :

 $ find | sort . ./Makefile ./README ./build ./client.c ./client.h ./common.h ./project.c ./server.c ./server.h ./tests ./tests/suite1.pl ./tests/suite2.pl ./tests/suite3.pl ./tests/suite4.pl 


If you want to output files like ls , you can add -ls :

 $ find -ls | sort -k 11 1155096 4 drwxr-xr-x 4 tom tom 4096 Feb 10 09:37 . 1155152 4 drwxr-xr-x 2 tom tom 4096 Feb 10 09:17 ./build 1155155 4 -rw-r--r-- 1 tom tom 2290 Jan 11 07:21 ./client.c 1155157 4 -rw-r--r-- 1 tom tom 1871 Jan 11 16:41 ./client.h 1155159 32 -rw-r--r-- 1 tom tom 30390 Jan 10 15:29 ./common.h 1155153 24 -rw-r--r-- 1 tom tom 21170 Jan 11 05:43 ./Makefile 1155154 16 -rw-r--r-- 1 tom tom 13966 Jan 14 07:39 ./project.c 1155080 28 -rw-r--r-- 1 tom tom 25840 Jan 15 22:28 ./README 1155156 32 -rw-r--r-- 1 tom tom 31124 Jan 11 02:34 ./server.c 1155158 4 -rw-r--r-- 1 tom tom 3599 Jan 16 05:27 ./server.h 1155160 4 drwxr-xr-x 2 tom tom 4096 Feb 10 09:29 ./tests 1155161 4 -rw-r--r-- 1 tom tom 288 Jan 13 03:04 ./tests/suite1.pl 1155162 4 -rw-r--r-- 1 tom tom 1792 Jan 13 10:06 ./tests/suite2.pl 1155163 4 -rw-r--r-- 1 tom tom 112 Jan 9 23:42 ./tests/suite3.pl 1155164 4 -rw-r--r-- 1 tom tom 144 Jan 15 02:10 ./tests/suite4.pl 


Notice, in this case, I had to give instructions to sort by 11 output columns, that is, by file names; use the -k switch to do this.

Find has a rather complex inline filter syntax. The following examples show some of the most useful ones you can use to get the list of files you need:


All of the above can be combined. For example, to find the source code for C, edited in the last 2 days, we write:

 $ find -name '*.c' -mtime -2 

Over the found files find can perform different actions. By default, this is the list direction to standard output, but there are several other options:


Other file search



And yet, much more often than by external attributes, it is required to look for files based on their contents, and here grep , and especially grep -R , hurries to the rescue. This command recursively searches in the current directory for anything that matches the specified text:

 $ grep -FR 'someVar' . 


Do not forget the case-insensitive flag, since by default grep works case sensitive.

 $ grep -iR 'somevar' . 


You can also display a list of files containing matches without the matching lines themselves using grep -l . This, again, is useful when building a list of files for further editing in a text editor:

 $ vim -O $(grep -lR 'somevar' .) 


If the project uses version control, then all the metadata is usually contained in the .svn , .git or .hg . You can easily eliminate ( grep -v ) unnecessary elements with a fixed-line comparison ( grep -F ):
 $ grep -R 'someVar' . | grep -vF '.svn' 

There is a very popular alternative to grep , called ack , which by default excludes all such husks. Ack also allows you to use Perl-compatible regular expressions (PCRE), so beloved by many hackers. It has very convenient tools for working with sources. Despite the fact that the good old grep has no problems, since it is always at hand, I strongly advise you to install Ack if possible. For all popular Unix systems, there is a corresponding package in the standard repositories.

The principal fans of Unix are most likely upset by the mention of such a relatively new alternative to grep , and also in the form of a Perl script. However, I would not like to think that the Unix philosophy and, in particular, the idea of ​​using Unix as an IDE necessarily means the abandonment of similar in spirit modern alternatives capable of solving actual problems.

File metadata



The file utility displays in one line a summary of the file based on extensions, headers, and other attributes. This is very useful when used together with find and xargs to study groups of unknown files:
 $ find | xargs file .: directory ./hanoi: Perl script, ASCII text executable ./.hanoi.swp: Vim swap file, version 7.3 ./factorial: Perl script, ASCII text executable ./bits.c: C source, ASCII text ./bits: ELF 32-bit LSB executable, Intel 80386, version ... 

Pattern Search


In the conclusion of this article, I would advise you to get acquainted with the search by template and disclosure of Bash brackets. My separate article is devoted to this question.

All of the above gives the Unix shell quite powerful file management tools when writing programs.

To be continued...

Unix as IDE: Introduction
Unix as IDE: Files
Unix as IDE: Working with Text
Unix as IDE: Compile

Source: https://habr.com/ru/post/151064/


All Articles