File management Linux Commands

File management in Linux is accomplished through the use of various commands. 

Here are some common Linux commands used for file management:


ls

The ls (list) command is used to list the contents of a directory in Linux. The basic syntax for ls is as follows:

ls [OPTIONS] [DIRECTORY]

Where '[OPTIONS]' are optional flags that modify the behavior of the 'ls' command and '[DIRECTORY]' is the name of the directory to be listed (the current directory by default).

Here are some common options for "ls":

  • -a: List all files, including hidden files and directories (those starting with a dot .).
  • -l: Display the contents in a long format, showing detailed information about each file or directory.
  • -h: Display file sizes in human-readable format (e.g. KB, MB, GB, etc.).
  • -r: Reverse the order of the listing.
  • -t: Sort the contents by modification time (newest first).

Example usage:

Inside a folder you can list all the files that the folder contains using the ls command:

username@Technoscience:~$ ls    # This command lists the contents of the current directory
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
username@Technoscience:~$

If you add a folder name or path, it will print that folder's contents:

username@Technoscience:~$ ls /bin/        #List the contents of the directory of /bin
archdetect              hostname               ping4 autopartition           hw-detect              ping6
autopartition-crypto    in-target              plymouth autopartition-loop      ip                     preseed_command
autopartition-lvm       journalctl             ps bash                    kbd_mode               pwd
block-attr              kill                   rbash blockdev-keygen         kmod                   readlink
blockdev-wipe           less                   red btrfs                   lessecho               register-module
btrfsck                 lessfile               rm btrfs-debug-tree        lesskey                rmdir
btrfs-find-root         lesspipe               rnano btrfs-image             list-devices           run-parts
btrfs-map-logical       ln                     search-path btrfs-select-super      loadkeys               sed
btrfstune               login                  select_mountoptions btrfs-zero-log          loginctl               select_mountpoint
bunzip2                 log-output             setfacl busybox                 lowntfs-3g             setfont
bzcat                   ls                     setupcon bzcmp                   lsblk                  sh
bzdiff                  lsmod                  sh.distrib bzegrep                 mapdevfs               sleep
bzexe                   mkdir                  ss bzfgrep                 mkfs.btrfs             static-sh
bzgrep                  mknod                  stty bzip2                   mktemp                 su
bzip2recover            more                   sync bzless                  mount                  sysfs-update-devnames
bzmore                  mountpoint             systemctl cat                     mt                     systemd
chacl                   mt-gnu                 systemd-ask-password check-missing-firmware  mv                     systemd-escape
chgrp                   nano                   systemd-hwdb chmod                   nc                     systemd-inhibit
chown                   nc.openbsd             systemd-machine-id-setup chvt                    netcat                 systemd-notify
cp                      netstat                systemd-sysusers cpio                    networkctl             systemd-tmpfiles
dash                    nisdomainname          systemd-tty-ask-password-agent date                    ntfs-3g                tar
dd                      ntfs-3g.probe          tempfile debconf-get             ntfscat                touch
debconf-set             ntfscluster            true df                      ntfscmp                udevadm
dmesg                   ntfsfix                umount dir                     ntfsfallocate          ulockmgr_server
dnsdomainname           ntfsinfo               uname domainname              ntfsls                 uncompress
dumpkeys                ntfsmove               unicode_start echo                    ntfsrecover            update-dev
ed                      ntfssecaudit           user-params egrep                   ntfstruncate           vdir
false                   ntfsusermap            wdctl fetch-url               ntfswipe               which
fgconsole               open                   whiptail fgrep                   openvt                 ypdomainname
findmnt                 parted_devices         zcat fsck.btrfs              parted_server          zcmp
fuser                   partman                zdiff fusermount              partman-command        zegrep
getfacl                 partman-commit         zfgrep get_mountoptions        partmap                zforce
grep                    perform_recipe         zgrep gunzip                  perform_recipe_by_lvm  zless
gzexe                   pidof                  zmore gzip                    ping                   znew
username@Technoscience:~$

This command lists the contents of the directory /bin/ in a long format with human-readable file sizes.

"ls" accepts a lot of options. One of my favorite options combinations is -al. Try it:

username@Technoscience:~$ ls -al /bin/
total 15624
drwxr-xr-x  2 root root    4096 Mar 12  2020 .
drwxr-xr-x 23 root root    4096 Feb  1 18:11 ..
-rwxr-xr-x  1 root root    6216 Mar 23  2019 archdetect
-rwxr-xr-x  1 root root    1091 Feb 23  2018 autopartition
-rwxr-xr-x  1 root root    1866 Nov  9  2017 autopartition-crypto
-rwxr-xr-x  1 root root    8684 Feb  2  2012 autopartition-loop
-rwxr-xr-x  1 root root     139 Aug 12  2014 autopartition-lvm
-rwxr-xr-x  1 root root 1113504 Jun  7  2019 bash
-rwxr-xr-x  1 root root     495 Dec 30  2017 block-attr
-rwxr-xr-x  1 root root    4683 Dec 23  2015 blockdev-keygen
-rwxr-xr-x  1 root root   10104 Feb  3  2020 blockdev-wipe
-rwxr-xr-x  1 root root  716464 Mar 13  2018 btrfs
lrwxrwxrwx  1 root root       5 Feb  1 18:01 btrfsck -> btrfs
-rwxr-xr-x  1 root root  375952 Mar 13  2018 btrfs-debug-tree
-rwxr-xr-x  1 root root  371856 Mar 13  2018 btrfs-find-root
-rwxr-xr-x  1 root root  396432 Mar 13  2018 btrfs-image
-rwxr-xr-x  1 root root  375952 Mar 13  2018 btrfs-map-logical
<SNIP>

compared to the plain ls, this returns much more information. 

You have, from left to right:

  • the file permissions (and if your system supports ACLs, you get an ACL flag as well)
  • the number of links to that file
  • the owner of the file
  • the group of the file
  • the file size in bytes
  • the file modified datetime
  • the file name

This set of data is generated by the l option. The “a” option instead also shows the hidden files. 

Note: Hidden files are files that start with a dot ( . )


cd

The cd (change directory) command is used to change the current working directory in Linux. The basic syntax for cd is as follows:

cd [DIRECTORY]

Where [DIRECTORY] is the name of the directory to change to.

Here are some special directory references that can be used with cd:
  • ".": The current directory.
  • "..": The parent directory of the current directory.
  • "~": The home directory of the current user.

Example usage:

Once you have a folder, you can move into it using the cd command. “cd” means to change the directory. You invoke it by specifying a folder to move into. You can specify a folder name or an entire path.

username@Technoscience:~$ mkdir newdir
username@Technoscience:~$ cd newdir/
username@Technoscience:~/newdir$

Now you are in the "newdir" folder.

You can use the ".." special path to indicate the parent folder:

username@Technoscience:~/newdir$ cd ..    # Back to the home directory
username@Technoscience:~$

The # character indicates the start of the comment, which lasts for the entire line after it's found.

You can use it to form a path:

username@Technoscience:~$ mkdir newdir
username@Technoscience:~$ mkdir newdir1
username@Technoscience:~$ cd newdir
username@Technoscience:~/newdir$ cd ../newdir1
username@Technoscience:~/newdir1$

There is another special path indicator which is “.”, and indicates the current folder. You can also use absolute paths, which start from the root folder “/”:

username@Technoscience:~/newdir1$ cd /etc
username@Technoscience:/etc$

This command changes the current working.


cp

The cp (copy) command is used to copy files and directories in Linux. The basic syntax for cp is as follows:

cp [OPTIONS] SOURCE DESTINATION

Where [OPTIONS] are optional flags that modify the behavior of the cp command, SOURCE is the file or directory to be copied, and DESTINATION is the location to copy the SOURCE to.

Here are some standard options for "cp":

  • -a or --archive: Copy the contents of a directory recursively and preserve the original file attributes (such as permissions, timestamps, etc.).
  • -r or -R: Recursively copy a directory and its contents.
  • -v: Verbose, display progress, and other information during the copy operation.
  • -i: Interactive, prompt for confirmation before overwriting an existing file.

Example usage:

You can copy a file using the cp command:

username@Technoscience:~$ touch file.txt
username@Technoscience:~$ cp file.txt newfile.txt      # Copy the file "file.txt" to new file named "newfile.txt"
username@Technoscience:~$

To copy folders you need to add the -r option to recursively copy the whole folder contents:

username@Technoscience:~$ mkdir dir
username@Technoscience:~$ cp -r dir newdir        # Recursively copies the directory "dir" to new directory named "newdir"
username@Technoscience:~$


mv

The mv (move) command is used to move or rename files and directories in Linux. The basic syntax for mv is as follows:

mv [OPTIONS] SOURCE DESTINATION

Where [OPTIONS] are optional flags that modify the behavior of the mv command, SOURCE is the file or directory to be moved, and DESTINATION is the location to move the SOURCE to or the new name for the SOURCE if it is being renamed.

Here are some standard options for mv:

  • -v: Verbose, display progress, and other information during the move operation.
  • -i: Interactive, prompt for confirmation before overwriting an existing file.
  • -f: Force move, overwrite existing files without confirmation.

Example usage:

Once you have a file, you can move it around using the "mv" command. You specify the file's current path and its new path:

username@Technoscience:~$ touch file.txt
username@Technoscience:~$ mv file.txt newfile.txt        # Rename the file "file.txt" to "newfile.txt"
username@Technoscience:~$ 

This is how you rename files and folders. If you have two directories, you want to move, use the following command:

username@Technoscience:~$ mkdir dir newdir
username@Technoscience:~$ mv dir newdir    #Move the directory "dir" to a new location "newdir"
username@Technoscience:~$

If the last parameter is a folder, the file located at the first parameter path is going to be moved into that folder. In this case, you can specify a list of files and they will all be moved to the folder path identified by the last parameter:

username@Technoscience:~$ touch file1.txt
username@Technoscience:~$ touch file2.txt
username@Technoscience:~$ mkdir dir
username@Technoscience:~$ mv file1.txt file2.txt dir    # Move multiple files "file1.txt" and "file2.txt" to the directory "dir"
username@Technoscience:~$


rm

The rm (remove) command is used to delete files or directories in Linux. The basic syntax for rm is as follows:

rm [OPTIONS] [FILE]

Where [OPTIONS] are optional flags that modify the behavior of the rm command and [FILE] is the name of the file or directory to be deleted.

Here are some common options for rm:

  • -f: Force deletion, ignoring any prompts or errors.
  • -r or -R: Recursively delete a directory and its contents.
  • -v: Verbose, display progress and other information during deletion.
  • --interactive: Prompt for confirmation before deleting each file.

It is important to use "rm" with caution, as once a file or directory is deleted, it cannot be recovered without a backup or data recovery tool.

Example usage:

Once you have a file, you can remove it using the "rm" command:

username@Technoscience:~$ touch file.txt
username@Technoscience:~$ rm file.txt        # Deletes the file "file.txt"
username@Technoscience:~$

You can also remove a directory using the "rm" command:

username@Technoscience:~$ mkdir dir
username@Technoscience:~$ rm -r dir        # Recursively deletes the directory dir and its contents
username@Technoscience:~$


mkdir

The mkdir (make directory) command is used to create new directories in Linux. The basic syntax for mkdir is as follows:

mkdir [OPTIONS] DIRECTORY

Where [OPTIONS] are optional flags that modify the behavior of the mkdir command and DIRECTORY is the name of the directory to be created.

Here are some common options for mkdir:
  • -p: Create intermediate directories as necessary.
  • -v: Verbose, and display progress and other information during the directory creation.

Example usage:

You create folders using the "mkdircommand:

username@Technoscience:~$ mkdir newdir    # Create a new directory named "newdir"
username@Technoscience:~$

You can create multiple folders with one command:

username@Technoscience:~$ mkdir dir1 dir2
username@Technoscience:~$

You can also create multiple nested folders by adding the -p option:

username@Technoscience:~$ mkdir -p a/b/c 
username@Technoscience:~$
This command creates a directory structure a/b/c with intermediate directories "a" and "b" if they do not already exist.

Options in UNIX commands commonly take this form. You add them right after the command name, and they change how the command behaves. You can often combine multiple options, too.

You can find which options a command supports by typing man <command-name>. Try now with man mkdir for example (press the q key to esc the man page). Man pages are the amazing built-in help for UNIX.


rmdir

The rmdir (remove directory) command is used to remove empty directories in Linux. The basic syntax for rmdir is as follows:

rmdir [OPTIONS] DIRECTORY

Where [OPTIONS] are optional flags that modify the behavior of the rmdir command and DIRECTORY is the name of the directory to be removed.

Here are some common options for rmdir:
  • -v: Verbose, display progress, and other information during the directory removal.

Example usage:

Just as you can create a folder using mkdir, you can delete a folder using rmdir :

username@Technoscience:~$ mkdir emptydir    # Create a directory named "emptydir"
username@Technoscience:~$ rmdir emptydir    # Remove the empty directory "emptydir"
username@Technoscience:~$

You can also delete multiple folders at once:

username@Technoscience:~$ mkdir dir1 dir2
username@Technoscience:~$ rmdir dir1 dir2
username@Technoscience:~$

Note: The folder you delete must be empty

To delete folders with files in them, we'll use the more generic "rmcommand which deletes files and folders, using the "-rfoptions:

username@Technoscience:~$ rmdir -rf newdir
username@Technoscience:~$

Be careful as this command does not ask for confirmation and it will immediately remove anything
you ask it to remove. 

There is no bin when removing files from the command line, and recovering lost files can be hard.

Note: Directories that contain files cannot be removed with "rmdir". To remove a non-empty directory, use the "rm" command with the "-r" option to remove the directory and its contents recursively.


touch

The touch command is used to create new empty files or update the modification timestamps of existing files in Linux. The basic syntax for touch is as follows:

touch [OPTIONS] FILE

Where [OPTIONS] are optional flags that modify the behavior of the touch command and FILE is the name of the file to be created or updated.

Here are some common options for touch:

  • -a: Only update the access time of the file.
  • -m: Only update the modification time of the file.
  • -c: Do not create a file if it does not exist (no-create).

Example usage:

You can create an empty file using the touch command:

username@Technoscience:~$ touch newfile.txt
username@Technoscience:~$

This command creates a new empty file named "newfile.txt" if it does not already exist, or updates its modification timestamp if it exists.

username@Technoscience:~$ touch -a file.txt
username@Technoscience:~$ 

This command updates the access timestamp of the file "file.txt" without modifying its modification timestamp.


chmod

The chmod (change mode) command is used to change the permissions of files and directories in Linux. The basic syntax for chmod is as follows:

chmod [OPTIONS] MODE FILE

Where [OPTIONS] are optional flags that modify the behavior of the chmod command, MODE is the new permission set for the file or directory, and FILE is the name of the file or directory whose permissions will be changed.

The permissions on a file or directory in Linux can be represented as a three-digit octal number, where each digit represents the permissions for the owner, group, and others, respectively. Each digit is a combination of read (r), write (w), and execute (x) permissions. 

For example, rwx means to read, write, and execute permissions, while r-x means to read and execute but not write permission.

The weird strings you see on each file line, like drwxr-xr-x, define the permissions of the file or folder. 

Let's dissect it. 

The first letter indicates the type of file:

  • "-" means it's a normal file
  • "d" means it's a directory
  • "l" means it's a link

Then you have 3 sets of values:

  • The first set represents the permissions of the owner of the file
  • The second set represents the permissions of the members of the group the file is associated to
  • The third set represents the permissions of everyone else

Those sets are composed of 3 values. rwx means that a specific persona has read, write, and execution access.

Anything that is removed is swapped with a "-", which lets you form various combinations of values and relative permissions: rw-, r--, r-x, and so on.

You can change the permissions given to a file using the chmod command. chmod can be used in 2 ways. The first uses symbolic arguments, and the second is using numeric arguments. Let's start with symbols first, which is more intuitive. 

You type chmod followed by a space and a letter:
  • "a" stands for all
  • "u" stands for user
  • "g" stands for the group
  • "o" stands for others

Then you type either '+' or '-' to add permission or to remove it. Then you enter one or more permissions symbols ( r, w, x ). All are followed by the file or folder name. 

Example Usage:

We have a file named "newfile.txt" which has read and write permission for users and groups and only readable permission for others.

username@Technoscience:~/newdir$ touch newfile.txt
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 01:49 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rw-rw-r--  1 username username    0 Feb  2 01:49 newfile.txt
username@Technoscience:~/newdir$

To add executable permission for everyone use the following command:

username@Technoscience:~/newfile$ chmod a+x newfile.txt
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:11 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rwxrwxr-x  1 username username    0 Feb  2 02:11 newfile.txt
username@Technoscience:~/newfile$ 

To add read and write to everyone use the following command:

username@Technoscience:~/newfile$ chmod a+rw newfile.txt
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:11 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rwxrwxrwx  1 username username    0 Feb  2 02:11 newfile.txt
username@Technoscience:~/newfile$

To remove read, write, and Executable permissions from other permissions use the following command:

username@Technoscience:~/newfile$ chmod o-rwx newfile.txt
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:11 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rwxrwx---  1 username username    0 Feb  2 02:11 newfile.txt
username@Technoscience:~/newfile$

You can apply the same permissions to multiple permissions by adding multiple letters before the + / - :

username@Technoscience:~/newfile$ chmod og-r newfile.txt
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:11 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rwx-wx---  1 username username    0 Feb  2 02:11 newfile.txt
username@Technoscience:~/newfile$

In case you are editing a folder, you can apply the permissions to every file contained in that folder using the -r (recursive) flag.

Numeric arguments are faster but I find them hard to remember when you are not using them day to day. You use a digit that represents the permissions of the persona. This number value can be a maximum of 7, and it's calculated in this way:

  • 1 if has execution permission
  • 2 if has to write permission
  • 4 if has read permission
This gives us 4 combinations:
  • 0 no permissions
  • 1 can execute
  • 2 can write
  • 3 can write, execute
  • 4 can read
  • 5 can read, execute
  • 6 can read, write
  • 7 can read, write and execute

We use them in pairs of 3, to set the permissions of all the 3 groups altogether:

username@Technoscience:~/newfile$ touch file.sh
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:26 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rw-rw-r--  1 username username    0 Feb  2 02:26 file.sh
username@Technoscience:~/newfile$ chmod 777 file.sh
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:26 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rwxrwxrwx  1 username username    0 Feb  2 02:26 file.sh
username@Technoscience:~/newfile$

This command adds execute permission for all users for the file "file.sh".


chown

The chown (change owner) command is used to change the owner and/or group of files and directories in Linux. The basic syntax for chown is as follows:

chown [OPTIONS] OWNER[:GROUP] FILE

Where [OPTIONS] are optional flags that modify the behavior of the chown command, OWNER is the username or numeric user ID of the new owner of the file or directory, GROUP is the name or numeric group ID of the new group for the file or directory, and FILE is the name of the file or directory whose owner and/or group will be changed.

Here are some standard options for chown:

  • -R: Recursively change the owner and/or group of files and directories in a directory and its subdirectories.

Example Usage: 

If you have a file that's owned by root, you can't write to it as another user:

username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 03:39 .
drwxr-xr-x 18 username username 4096 Feb  2 03:39 ..
-rw-r--r--  1 root     root        0 Feb  2 03:39 testfile.txt
username@Technoscience:~/newdir$

You can use chown to transfer the ownership to you:

username@Technoscience:~/newdir$ sudo chown username testfile.txt     # Change the Owner of the file "testfile.txt" to user "username"
[sudo] password for username:
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 03:39 .
drwxr-xr-x 18 username username 4096 Feb  2 03:39 ..
-rw-r--r--  1 username root        0 Feb  2 03:39 testfile.txt
username@Technoscience:~/newdir$

Files/directories don't just have an owner, they also have a group. Through this command, you can change that simultaneously while you change the owner:

username@Technoscience:~/newdir$ sudo chown username:username testfile.txt
username@Technoscience:~/newdir$

This command changes both the owner and group of the file "testfile.txt" to user and group respectively.

username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 03:39 .
drwxr-xr-x 18 username username 4096 Feb  2 03:39 ..
-rw-r--r--  1 username root        0 Feb  2 03:39 testfile.txt
username@Technoscience:~/newdir$

It's rather common to have the need to change the ownership of a directory, and recursively all the files contained, plus all the subdirectories and the files contained in them, too. 

You can do so using the -R flag:

username@Technoscience:~$ chown -R username:group newdir
username@Technoscience:~$ 

You can also just change the group of a file using the chgrp command:

username@Technoscience:~$ chgrp groupname newdir
username@Technoscience:~$ 


ln

The ln (link) command is used to create hard links or symbolic links in Linux. The basic syntax for creating a hard link is as follows:

ln [OPTIONS] TARGET NEW_LINK

Where [OPTIONS] are optional flags that modify the behavior of the ln command, TARGET is the name of the original file or directory, and NEW_LINK is the name of the new hard link to be created.

Here are some standard options for ln:

  • -s: Create a symbolic link instead of a hard link.

The ln command is part of the Linux file system commands.

It's used to create links. 

What is a link?

It's like a pointer to another file. A file that points to another file. You might be familiar with Windows shortcuts. 

We have 2 types of links: 

  1. Hard links
  2. Soft links

1. Hard links

Hard links are rarely used. They have a few limitations: you can't link to directories, and you can't
link to external filesystems (disks).

A hard link is created using the following command:

username@Technoscience:~/newdir$ touch file.txt
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 11:13 .
drwxr-xr-x 18 username username 4096 Feb  2 11:11 ..
-rw-rw-r--  1 username username    0 Feb  2 11:13 file.txt
username@Technoscience:~/newdir$ ln file.txt link_file.txt    # Create a Hard link named "link_file.txt" that points to the original file "file.txt"
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 11:16 .
drwxr-xr-x 18 username username 4096 Feb  2 11:11 ..
-rw-rw-r--  2 username username    0 Feb  2 11:13 file.txt
-rw-rw-r--  2 username username    0 Feb  2 11:13 link_file.txt
username@Technoscience:~/newdir$

Now any time you edit any of those files, the content will be updated for both. 

If you delete the original file, the link will still contain the original file content, as that's not removed until there is one hard link pointing to it.


2. Soft links

Soft links are different. They are more powerful as you can link to other filesystems and to directories, but when the original is removed, the link will be broken. You create soft links using the -s option of "ln" :

Example Usage:

Say you have a file called file.txt. You can create a soft link to it using the following command:

username@Technoscience:~/newdir$ touch file.txt
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 11:26 .
drwxr-xr-x 18 username username 4096 Feb  2 11:11 ..
-rw-rw-r--  1 username username    0 Feb  2 11:26 file.txt
username@Technoscience:~/newdir$ ln -s file.txt sym_link_file.txt
username@Technoscience:~/newdir$

In this case, you can see there's a special “l” flag when you list the file using ls -al, and the file name has a @ at the end, and it's colored differently if you have colors enabled:

username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 11:26 .
drwxr-xr-x 18 username username 4096 Feb  2 11:11 ..
-rw-rw-r--  1 username username    0 Feb  2 11:26 file.txt
lrwxrwxrwx  1 username username    8 Feb  2 11:26 sym_link_file.txt -> file.txt
username@Technoscience:~/newdir$

Now if you delete the original file, the links will be broken, and the shell will tell you "No such file or
directory
" if you try to access it:

username@Technoscience:~/newdir$ rm file.txt
username@Technoscience:~/newdir$ cat sym_link_file.txt
cat: sym_link_file.txt: No such file or directory
username@Technoscience:~/newdir$


find

The "find" command is used to search for files and directories in a specified location. The basic syntax for the "find" command is as follows:

find [OPTIONS] [LOCATION] [EXPRESSION]

Where [OPTIONS] are optional flags that modify the behavior of the find command, [LOCATION] is the starting directory for the search, and [EXPRESSION] is a set of conditions that specify the files and directories to be found.

Here are some standard options for "find":

  • -name: Search for files and directories with a specific name.
  • -type: Search for files and directories of a specific type (e.g. f for files, d for directories).
  • -mtime: Search for files and directories modified within a certain number of days ago.
  • -print: Display the names of the found files and directories.

Example usage:

Find all the files under the current tree that have the " .jsextension and print the relative path of each file matching:

username@Technoscience:~$ find . -name '*.js'

It's important to use quotes around special characters like '*to avoid the shell interpreting them.

Find directories under the current tree matching the name "src":

username@Technoscience:~$ find . -type d -name src

Use -type f to search only files, or -type l to only search symbolic links. 

-name is case sensitive. use -iname to perform a case-insensitive search.

You can search under multiple root trees:

username@Technoscience:~$ find folder1 folder2 -name filename.txt

Find directories under the current tree matching the name "node_modules" or 'public':

username@Technoscience:~$ find . -type d -name node_modules -or -name public

You can also exclude a path, using -not -path :

username@Technoscience:~$ find . -type d -name '*.md' -not -path 'node_modules'

You can search files that have more than 100 characters (bytes) in them:

username@Technoscience:~$ find . -type f -size +100c

Search files bigger than 100KB but smaller than 1MB:

username@Technoscience:~$ find . -type f -size +100k -size -1M

Search files edited more than 3 days ago:

username@Technoscience:~$ find . -type f -mtime +3

Search files edited in the last 24 hours:

username@Technoscience:~$ find . -type f -mtime -1

You can delete all the files matching a search by adding the -delete option. This deletes all the files
edited in the last 24 hours:

username@Technoscience:~$ find . -type f -mtime -1 -delete

You can execute a command on each result of the search. In this example, we run cat to print the file
content:

username@Technoscience:~$ find . -type f -exec cat {} \;

notice the terminating " \; ". {} is filled with the file name at execution time.


locate

The 'locate' command is used to find files and directories quickly on a Linux system. It works by searching an index of filenames, which is generated by the updatedb command, rather than by scanning the file system itself. The basic syntax for the locate command is as follows:

locate [OPTIONS] [PATTERN]

Where [OPTIONS] are optional flags that modify the behavior of the locate command, and [PATTERN] is a string that specifies the name of the files and directories to be found.

Here are some standard options for "locate":

  • -i: Perform a case-insensitive search.
  • -c: Display the number of matching results, but not the results themselves.

Example usage:

If you want to search any file use the following command:

username@Technoscience:~$ locate file.txt    # Finds all files and directories named file "file.txt"

To finds all files and directories named file.txt or FILE.TXT on the system, ignoring the case:

username@Technoscience:~$ locate -i file.txt

To display, the number of files and directories named file.txt on the system, but not the names themselves.

username@Technoscience:~$ locate -c file.txt


which

The "which" command is used to determine the location of an executable file in the system's PATH. The basic syntax for the which command is as follows:

which [COMMAND]

Where [COMMAND] is the name of the executable file to be found.

Example usage:

You can do so using 'which'. The command will return the path to the command specified:

username@Technoscience:~$ which ls    # Displays the full path to the ls executable file on the system
/bin/ls
username@Technoscience:~$

which will only work for executables stored on disk, not aliases or built-in shell functions.


whereis

whereis command is used to find the location of the source/binary file of a command and manuals sections for a specified file in a Linux system. If we compare the "whereis" command with the find command they will appear similar to each other as both can be used for the same purposes but the "whereis" command produces the result more accurately by consuming less time comparatively.

whereis [OPTIONS] filename

Where [OPTIONS] are optional flags that modify the behavior of the locate command.

Here are some standard options for whereis:

  • -b : This option is used when we only want to search for binaries.
  • -m : This option is used when we only want to search for manual sections. 
  • -B: This option is used to change or otherwise limit the places where whereis searches for binaries.
  • -M: This option is used to change or otherwise limit the places where whereis searches for manual sections. 
  • -S: This option is used to change or otherwise limit the places where whereis searches for sources.

Example Usage:

 To find the location of the source/binary file of a command uses the following command:

username@Technoscience:~$ whereis gunzip
gunzip: /bin/gunzip /usr/share/man/man1/gunzip.1.gz
username@Technoscience:~$

To locate the binary of a Linux command let’s say gunzip:

username@Technoscience:~$ whereis -b gunzip
gunzip: /bin/gunzip
username@Technoscience:~$

Each of these commands has various options and flags that can be used to customize their behavior, so it is important to consult the manual pages for these commands for more information.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!