Questions tagged [files]

Manipulating files: copying, renaming, searching, analyzing, archiving, etc. For operating on text in a file, see /text-processing. For questions about the Files file manager (formerly Nautilus), see /nautilus.

A computer file is a logical entity that resides in a filesystem and is either empty or contains data. Depending on the filesystem, files have certain attributes, such as: (full) path, name, permissions, size, etc. Files are typically arranged in a set of folders or directories to separate them by purpose.

For the Gnome file manager Files, formerly known as Nautilus, see .


We have a canonical question with several good examples that asks:

How can I replace a string in a file(s)?


There are many tags on this site that are related to files; use these either along-side or in place of the files tag:

  • - either generically renaming files or the prename (perl rename) utility
  • - a utility to find files
  • - who can read, write, and execute a file
  • - logical container for a set of files
  • - contains files and directories on a storage device
  • - lists files and their attributes
  • - removes a file
  • - renames one or more files
  • - special syntax for a shell to specify more than one file, using a pattern
  • - special link to another file
  • - files that contain messages from one or more programs
  • - name of a file
  • - copying files from one place to another (e.g. cp, rsync, dd)

There are other tags on this site that deal with the processing of the data in files; use them in place of this tag:

  • - performing some operation (modifying, adding, deleting) on text in a file
  • - presenting the text in a file in a different way
4934 questions
502
votes
14 answers

How do I loop through only directories in bash?

I have a folder with some directories and some files (some are hidden, beginning with dot). for d in *; do echo $d done will loop through all files and directories, but I want to loop only through directories. How do I do that?
rubo77
  • 27,777
  • 43
  • 130
  • 199
404
votes
13 answers

How can I get the size of a file in a bash script?

How can I get the size of a file in a bash script? How do I assign this to a bash variable so I can use it later?
haunted85
  • 4,201
  • 2
  • 15
  • 9
401
votes
9 answers

How to get over "device or resource busy"?

I tried to rm -rf a folder, and got "device or resource busy". In Windows, I would have used LockHunter to resolve this. What's the linux equivalent? (Please give as answer a simple "unlock this" method, and not complete articles like this one.…
ripper234
  • 31,063
  • 43
  • 82
  • 90
368
votes
12 answers

How to list all files ordered by size

I would like to list all files in the order of big to small in size and the files could be present anywhere in a certain folder.
Joe
  • 3,813
  • 2
  • 13
  • 4
314
votes
7 answers

What's the point in adding a new line to the end of a file?

Some compilers (especially C or C++ ones) give you warnings about: No new line at end of file I thought this would be a C-programmers-only problem, but github displays a message in the commit view: \ No newline at end of file for a PHP file. I…
Philipp Stephan
  • 3,250
  • 2
  • 15
  • 8
311
votes
1 answer

How to change where a symlink points

Can you tell me what this is in the area marked red? If I want to change /media/files/tb-prod/files to some other path, how would I do that?
Jalil Khan
  • 3,221
  • 2
  • 12
  • 11
283
votes
3 answers

Most efficient method to empty the contents of a file

I am aware of three methods to delete all entries from a file. They are >filename touch filename1 filename < /dev/null Of these three I abuse >filename the most as that requires the least number of keystrokes. However, I would like to know which…
debal
  • 3,664
  • 5
  • 17
  • 18
281
votes
24 answers

Efficiently delete large directory containing thousands of files

We have an issue with a folder becoming unwieldy with hundreds of thousands of tiny files. There are so many files that performing rm -rf returns an error and instead what we need to do is something like: find /path/to/folder -name "filenamestart*"…
Toby
  • 3,973
  • 4
  • 20
  • 16
280
votes
9 answers

How can I display the contents of a text file on the command line?

I would like to display the contents of a text file on the command line. The file only contains 5-6 characters. Is there an easy way to do this?
Sam Weinberg
  • 2,913
  • 2
  • 13
  • 5
268
votes
3 answers

Get file created/creation time?

Possible Duplicate: How do I do a ls and then sort the results by date created? Is there a command in Linux which displays when the file was created ? I see that ls -l gives the last modified time, but can I get the created time/date?
linuxAddict
261
votes
4 answers

How to tell gzip to keep original file?

I would like to compress a text file using gzip command line tool while keeping the original file. By default running the following command gzip file.txt results in modifying this file and renaming it file.txt.gz. instead of this behavior I would…
Manuel Selva
  • 3,454
  • 7
  • 20
  • 17
248
votes
2 answers

Unzipping a .gz file without removing the gzipped file

I have a file file.gz, when I try to unzip this file by using gunzip file.gz, it unzipped the file but only contains extracted and removes the file.gz file. How can I unzip by keeping both unzipped file and zipped file?
jack
  • 3,853
  • 11
  • 28
  • 33
245
votes
14 answers

Find the total size of certain files within a directory branch

Assume there's an image storage directory, say, ./photos/john_doe, within which there are multiple subdirectories, where many certain files reside (say, *.jpg). How can I calculate a summary size of those files below the john_doe branch? I tried du…
mbaitoff
  • 4,911
  • 8
  • 28
  • 32
244
votes
11 answers

How can I edit multiple files in Vim?

I know I can open multiple files with vim by doing something like vim 2011-12*.log, but how can I switch between files and close the files one at a time? Also, how can I tell the file name of the current file that I'm editing?
cwd
  • 44,479
  • 71
  • 146
  • 167
237
votes
7 answers

How to move and overwrite subdirectories (and files) to parent directory?

I have a ton of files and dirs in a subdirectory I want to move to the parent directory. There are already some files and dirs in the target directory which need to be overwritten. Files that are only present in the target should be left untouched.…
EricSchaefer
  • 2,803
  • 2
  • 20
  • 20
1
2 3
99 100