14.7k views
1 vote
Write down the commands for the following questions:

1) redirect file1 to file2
2) redirect file1 and file2 to file2. What contents will file2 have? Why?
3) Append file1 to the end of file2
4) Search a file named ‘file1’ from the root and redirect the error message to /dev/null
5) Run the commands ls, and echo "hello" in one line

User Rhand
by
8.2k points

1 Answer

6 votes

Final answer:

Commands for various file operations and searching in Unix can be achieved using 'cp', 'cat', 'find', 'echo', and by combining commands using semicolons. Overwriting and appending files are done with '>' and '>>', while redirecting errors uses '2>'. Searching a file from the root and ignoring error messages involves 'find' and redirecting to '/dev/null'.

Step-by-step explanation:

To perform the tasks specified, the following commands can be used on a Unix-based system or in a Unix-like command-line environment:

  1. Redirect file1 to file2:
    cp file1 file2 or cat file1 > file2
  2. Redirect file1 and append file2 to itself: This is a trick question because you cannot redirect a file to itself without overwriting it. The command will replace the contents of file2 with file1 if you try cat file1 > file2. The original contents of file2 will be lost.
  3. Append file1 to the end of file2:
    cat file1 >> file2
  4. Search a file named 'file1' from the root and redirect the error message to /dev/null:
    find / -name file1 2>/dev/null
  5. Run the commands ls, and echo "hello" in one line:
    ls; echo "hello"

If you perform the second task, file2 will contain only the contents of file1 after the command because the greater than symbol (>) is used for overwriting, not appending.

User Jewelwast
by
7.8k points