Question: Here is what I want to do...

I have a bunch of text files that I need to go through and keep only the top 5 lines. If I do `top` then output to a file that is part way there. I need the file name to be the same as it was earlier.

Ex.: `file.name` - take top 5 lines (delete everything below line 5), then save file as `file.name`. There are a number of files that this will need to be done on as a batch.

Answer: 
You can use `head` to do this.

```
head  -n 5  file.name > file.name.tmp && mv file.name.tmp file.name

```

You can structure this as a script to do it for all files, passing `file.name` as an argument in each run later.
The basic syntax, assuming you are working with a file called "file.name", is quite simple

```
head -n5 file.name > temp && cat temp > file.name

```

or

```
cat file.name | head -n5 > temp && cat temp > file.name

```

If you are trying to do this on a lot of files in a path, you should use something like

```
find ./ -type f -printf "%f\n"

```

You can modify the parameters depending on what type of files you want

```
find ./ -name "*.js" -type f -printf "%f\n"

```

You can then iterate through that list, passing each line as an argument into the code above

It would probably look something like this (I haven't tested this, but it is similar to another small script I wrote, so hopefully it points you in the right direction)

```
function cutfile {
    head -n5 "$1" > temp && cat temp > "$1"
}

find ./ -name "*.js" -type f -printf "%f\n" |\
while read line; do cutfile $line; done

```

EDIT: Changed head...; cat... code based on @fedorqui's recommendations on @mu's code

EDIT: Fixed code, added "; done", made code more readable