Unix as IDE: Files

Original author: Tom Ryder
  • Transfer
One of the important features of the IDE is the integrated file management system. It should include both basic features, such as renaming, deleting, moving, 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 mask. In this first article, I will show you several uses of tools familiar to any Linux user for working with groups of files in a project.

File lists

The administrator gets acquainted 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 switches to display all files, including '.'and '..', and to display more detailed file information by column, respectively.

There are several more keys for ls , which are somewhat less commonly used, but very useful for programming purposes:
  • -t - List files arranged by the date of the last modification in descending order. The key is useful for very large directories when you need to quickly get a list of the latest 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 thereby rearrange 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 you to easily sort 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 a page leaflet like less .

Since the list of files is plain text, you can direct its stream to vim , where you add explanations to each file, and then save the text as an inventory or add it to README:

$ ls -XR | vim -

Such things can even be automated with make without much 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, simply by typing " find " with no arguments, although it is usually best to arrange them in order with sort :

$ find | sort

If you want to list 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

Note, in this case I had to give an order to sort by the 11th column of the output, that is, by the file names; the -k switch is used for this .

We find quite a complex integrated filter syntax. The following examples show some of the most useful that you can apply 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 searches.
  • find -path '*test*'- find files whose path matches the mask. You can use -ipath for case insensitive searches.
  • find -mtime -5- find files edited in the last 5 days. You can use +5 to find files modified earlier than 5 days ago.
  • find -newer server.c- find files modified earlier than 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 C sources edited over the last 2 days, we write:

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

Find can perform different actions on found files . By default, this directs the list to standard output, but there are several other options:
  • -ls - List in ls -l style .
  • -delete - Delete found files.
  • -exec - Execute an arbitrary command line on each file, where {} will be replaced with the corresponding file name, and \; , eg:
    $ find -name '*.pl' -exec perl -c {} \;

    In most cases, it’s easier to use xargs so that the result turns 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 a 0-character instead of a space will be assigned to the record separator. At the same time, xargs should start with the -0 switch :
    $ find -name '*.jpg' -print0 | xargs -0 jpegoptim

    I often use the following trick to generate lists, which can then be edited in Vim windows that divide the screen vertically:

    $ vim -O $(find . -name '*.c')

Other file search

Nevertheless, more often than by external attributes, it is required to search for files based on their contents, and here grep , and especially grep -R , is in a hurry to help. This command recursively searches the current directory for anything that matches the specified text:

$ grep -FR 'someVar' .

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

$ grep -iR 'somevar' .

You can also list files containing matches without matching lines themselves with grep -l. This, again, is convenient 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 metadata is usually contained in folders .svn, .gitor .hg. You can easily exclude ( grep -v) unnecessary elements by comparing with a fixed string ( grep -F):
$ grep -R 'someVar' . | grep -vF '.svn'

There is a very popular grep alternative called ack , which by default eliminates all similar husk. Ack also allows you to use the Perl-compatible regular expressions (PCRE), so beloved by many hackers. She has very convenient means of working with source codes. Despite the fact that there is no problem with the good old grep , since it is always at hand, I highly recommend installing Ack if possible . For all popular Unix systems, there is a corresponding package in standard repositories.

Fundamental Unix fans are most likely upset by the mention of such a relatively new alternative to grep., and even in the form of a Perl script. However, I would not want to think that the Unix philosophy and, in particular, the idea of ​​using Unix as an IDE necessarily means giving up on modern-looking alternatives that can solve current problems.

File metadata

The file utility displays in one line summary information about the file based on the extension, headers, and other attributes. This is very convenient when used with find and xargs to study groups of unfamiliar 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

To conclude this note, I would advise you to get a closer look at the search by template and the disclosure of Bash brackets. This is my separate article .

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

To be continued ...

Unix as IDE: Introduction of
Unix as IDE:
Unix Files as IDE: Working with Unix Text
as IDE: Compilation

Also popular now: