What are other uses of ~?

QuestionsQuestions8 SkillsProBasic Files OperationsNov, 28 2025
084

The tilde (~) has several uses in Bash and other Unix-like shells:

  1. Home Directory: As mentioned, ~ represents the current user's home directory.

    cd ~  # Changes to the home directory
  2. User Home Directory: You can specify another user's home directory by using ~username.

    cd ~john  # Changes to the home directory of user 'john'
  3. Path Expansion: It can be used in file paths to simplify navigation.

    ls ~/documents  # Lists files in the 'documents' folder in your home directory
  4. In Scripts: When writing scripts, ~ can be used to refer to the home directory dynamically, making scripts more portable across different users.

  5. Combining with Other Paths: You can combine ~ with other directory names to create paths relative to your home directory.

    cp ~/file.txt ~/backup/  # Copies 'file.txt' from home to 'backup' directory in home

If you have any more questions or need further examples, feel free to ask!

0 Comments

no data
Be the first to share your comment!