Page Content

Tutorials

What Is The Command Line Crash Course Manipulation in Ruby

Command Line Crash Course Manipulation in Ruby

To rapidly become proficient with the shell, the Command Line Crash Course places a strong emphasis on training and practice. The course then moves on to Manipulation and Review, where students learn how to create, copy, move, read, and delete files and directories after becoming proficient with fundamental navigation commands like pwd, cd, and ls (as we covered in our Pervious tutorial). Students are constantly told to do the exercises by hand, without copying and pasting, in order to develop their skills via practice.

You can also read Objects And Classes In Ruby Practical Object-Oriented Design

Essential commands for working with files and clearing the file system are introduced in the course’s manipulation section.

Command (UNIX/Windows)DescriptionKey Usage and Context
touch (UNIX) / New-Item (Windows)Creates an empty file.touch in UNIX also changes the modification times on a file. New-Item in Windows/PowerShell can also create new directories.
cpCopies a file or directory.Simple syntax involves specifying the source file and the destination file or directory (e.g., cp source.txt destination.txt). Note that cp will overwrite existing files. Use cp -r on UNIX to recursively copy directories.
mvMoves or renames a file or directory.Used by providing the old name/location followed by the new name/location (e.g., mv oldname.txt newname.txt). This command handles both moving files between directories and renaming them.
less (UNIX) / MORE (Windows)View the contents of a file one screenful at a time.Useful for long files as it “pages” the content. To exit less, the user types q (as in quit).
catStream or print the whole file content to the screen.This command dumps the entire file content to the screen without paging or stopping.
rmRemoves (deletes) a file permanently.This command deletes files. It is often combined with recursive options (-r or -recurse on Windows) to delete directories and their contents, which requires caution.

Manipulation Code Example

The subsequent command line session (in Linux/OS X notation) illustrates how to create, copy, rename (move), and inspect files:

$ cd temp
$ touch file1.txt            # Create an empty file
$ ls
file1.txt
$ cp file1.txt copy.txt      # Copy file1.txt to copy.txt
$ ls
copy.txt file1.txt
$ mv copy.txt renamed.txt    # Move/rename copy.txt
$ ls
file1.txt renamed.txt
$ cat renamed.txt            # Stream the file content (if empty, prints nothing)
$ rm renamed.txt file1.txt   # Remove both files
$ ls
$

You can also read Constructors In Ruby: How The Initialize Method Works

Command Line Crash Course: Review and Next Steps

Cleaning up the workspace and making sure the user knows how to leave the shell so they may continue studying are the goals of the crash course’s last steps.

Final Cleanup and Exiting

One essential command for cleanup is rm. Using rmdir on a directory that still has files in it would not work. It is possible to handle the contents and the directory at the same time by using the recursive delete option (e.g., rm -r on UNIX or rm -r or rm -recurse on Windows PowerShell) or by using rm to destroy the files.

The final exercise involves ending the terminal session using the exit command.

You can also read Instance variables In Ruby: What They Are & How To Use Them

Code Example (Removing Files and Exiting):

$ rm something/awesome.txt # Remove a file inside a directory
$ rmdir something          # Now remove the empty directory

# Or, to recursively remove a non-empty directory (DANGER):
$ rm -r newplace

# To exit the terminal:
$ exit

Continued Learning

After finishing the crash course, the user is regarded as a shell user with limited capabilities. The tasks in the last steps are intended to introduce the assistance system and promote independent study:

Researching New Commands: The help system is meant to be used by users to look up instructions that they should independently study and learn.

  • UNIX list: xargs, sudo, chmod, chown.
  • Windows list: forfiles, runas, attrib, icacls.

Updating Index Cards: Users should keep drilling after adding these new commands to their index cards.

Reviewing References: The source materials provide references for UNIX Bash and PowerShell-specific further education.

You can also read File I/O In Ruby: Basic Operations To Advanced File Handling

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index