
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:
- -t - Display files ranked by the last modification date in descending order. The key is useful for very large directories when you need to quickly get a list of the most recently edited files, possibly passed through head or sed 10q . Probably the most useful in conjunction with -l . If you need the oldest files, you can add -r and thus rebuild the list in reverse order.
- -X - Group files by extension. Useful for multilingual development, for separating header files from main code, for separating code files from directories, or assembly files.
- -v - Allows easy sorting of version numbers in file names.
- -S - Sort by file size.
- -R - list files recursively, including subdirectories. This key is good in conjunction with -l and redirecting output to the page list like less .
')
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:
find -name '*.c'
- find files with names according to the shell-style mask. You can use -iname for case-insensitive search.find -path '*test*'
- find files the path to which matches the mask. You can use -ipath for case-insensitive search.find -mtime -5
- find files edited in the last 5 days. You can use +5 to find files that were modified earlier than 5 days ago.find -newer server.c
- find files modified before server.c
.find -type d
- find directories. To search for files use -type f ; for symbolic links -type l
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:
- -ls - Print a list in ls -l style.
- -delete - Delete found files.
- -exec - Run an arbitrary command line on each file, where {} will be replaced with the corresponding file name, and the end of which is \; , eg:
$ find -name '*.pl' -exec perl -c {} \;
In most cases, it is easier to use xargs so that the result is turned into arguments for the command:
$ find -name '*.pl' | xargs perl -c
- -print0 - if you have files with spaces in the names, and you want to transfer them to xargs like the previous example, then use this key, and 0-character will be assigned as a separator instead of a space. At the same time, xargs should be launched with the -0 key:
$ find -name '*.jpg' -print0 | xargs -0 jpegoptim
I often use the following trick to generate lists, which can then be edited in the Vim windows dividing the screen vertically:
$ vim -O $(find . -name '*.c')
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: IntroductionUnix as IDE: FilesUnix as IDE: Working with TextUnix as IDE: Compile